OOPS in Java || Java and DSA course || Blog 15
OOPS stands for object oriented programming system. OOPS is common in every programming language whether it is c++ or java.
Modern languages are based on oops.
for a quick overview , we will be learning ;
- constructors
- Polymorphism
- Inheritance
- Access Modifiers
- Encapsulation
- Abstraction
what is an object and classes?
Everything in Java is associated with classes and objects, along with its attributes and methods.
A Class is like an object constructor, or a "blueprint" for creating objects.
lets take a student. Here, a student is an object while attributes like name, age, section is object attributes/classes.
the functions inside the classes are called members.
in the line Student student1 = new Student();
new stands for the memory allocation
Student() is a special type of function called Constructors.
Constructors
constructors construct objects. They cannot return .Their name should be same in the class . It calls only one time.
there are 3 types of constructors:
- Non parameterized
- Parameterized
- Copy constructors.
the student example we saw just now was an example of Non parameterized. we didn't pass any parameter inside the brackets.
Parameterized
Copy Constructors
these constructors copies someone else's data and prints it to us.
if we have constructors, we must haev destructors right!
well in java, we have our own garbage collector/destructors. it deletes stuff which is not used.
Polymorphism
it is made of 2 words. poly and morphism . poly means many, morphism means forms.
it means that we create many functions but it means the same.
lets take example as a car:
polymorphism works only when parameters are differewnt(int, string, boolean) or when types are different(void, int, return) or add parameters like car1.print(car1.name, car1.mileage);
there are two types of polymorphism, oveloading and overriding.
over loading is of compile type, it gives error while writing program, over riding gives error while running program.over riding is used a bit less because of this.
Inheritance
it passes properties from one class to another class
it stands for reusability, lets say you are making a website which has 4 buttons. they should be off same size. are we going to write them again and agian??NO. instead we are going to use Inheritance.
here another class takes all the attributes used in the class
here, btn1 can access both sizee() which is in the button function and printname().
there are 4 levels of Inheritance. what we saw here was single level inheritance.
Packages
Everyone likes clean house, where everything is at its place. just like that, we should also keep our programs clean so that if someones reading it, he/she will not get confuse. package works just like that. imagine it as a pecil case , where you keep all your pen and pencils.
packages are Built-in and user-defined.built in for example is java.uitl.*;
user defined packages are made by us.
simply write package package name;
then use import name.*; in other files to use the package.
Access Modifiers
it is used to define a boundary between who can red the data and who cant.
we have been using public for a long time.it is one type of access modifiers.
- Public - anyone can get it/ packages can get it.
- Private - only inside class
- Protected - in packages and in all files where package is importes
- default - It can be accessed from within the class, outside the class, within the package and outside the package.
Encapsulation- it is combining data and functions in the whole class.
Abstraction
two types
- abstract keyword
- interfaces
Data abstraction is the process of hiding certain details and showing only essential information to the user.
you cannot make an object of abstract class
it will run the pig says: wee wee /n secret
here we didn't knew what was in the what() but it ran.
Interfaces
An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.
interface shape{
....
}
class button implements animal{
.....
}
Comments
Post a Comment