Sei sulla pagina 1di 5

OOPS concept

Opps
JAva is a object oriented language
Four Features of OOPS
1. Encapsulation
2. Inheritance
3. Polymorphism
4. Abstraction
1. Encapsulation
Encapsulation means bringing all the variables and method in to single unit ca
lled class.
Define user define data type called class
class A{
int i;
char ch;
public void static main(String [] args) {}
}
2. Inheritance
Used for code re-usability..
3. Polymorphism
Polymorphism means MAny Form
1. Static Binding - it is checked at Compile time Example Function Overloading
2. Dynamic Binding - it is checked at run time
4. Abstraction
Abstraction - Something unknown.
1. Encapsulation
class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
}
A a1 = new A()
a1 memory allocated in stack
new A() allocated in heap
and a1 will pointing to the heap memomry
a1.i;
a1.get();
Constructors
Constructors will have same name as the class name and they donot have return ty
pe.

Constructors are used to initialize the members or do something with particular


instance when we careate the object.
when constructors are not define in the class then compiler inserts constructor
s called default constructor
default constructor is the constructor which will not take any parameter.
which will not take any parameter or argumets.
class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
A() {} -- Default constructor. We can aslo define linke this
}
when we add constructor then compiler will not insert default constructor
class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
A(int in)
{ i = in }-- parametrized constructor. now compiler will not insert default cons
tructor.
}
Constructors will executed when we create new ojects.
A= a1 = new A(10)

class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
void A(int in)
{ i = in }-- now this no more constructor. since it has return void. in this cla

ss there is no constructor
}

you can have more than one constructor.


class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
A(int in)
{ i = in} -- parametrized constructor. now compiler will not insert default cons
tructor.
A(int in, char c)
{ i = in; ch = c;} -- parametrized constructor. now compiler will not insert de
fault constructor.
}
2. Inheritance
class A{
int i;
char ch;
void set(()
{
i = 10;
}
int get()
{
return i;
}
class B extends A{
float f;
void disp(){}
}
B obj = new B();
If constructor is defined in Class B and call was not made to class A constructo
r then compiler inserts the super() keyword
at the beginning to call the parent default constructor.
if the parent class has parametrized constructor and do not have default constr
uctor
and in the class B constructor if there is no call for parent constructor then
we get compiler error.
So we have to call explicitly the parent constructor by using keyword super()
as a very first instruction.

and pass the value for the constructor parameter for example super(10)
it is a option to call parent constructor when you have default constructor in
parent class.
When we define a method in a child/sub class same as a parent class then it is
function overriding.
i.e. redefining the function which is defined in the parent class.
3. Polymorphism
Function Overloading
Same method name but different signature in the same class is Function Overloa
ding
in function overloading passing parameter has to be different does not mater wi
th return type.
so we cannot have function with same name but different return type not allowed
.
you can overload the method in same class or in child class.
if the method has same name and signature are same in the parent class method a
nd child class method then it is called function overriding
4. Abstraction
use the keyword abstract to define the abstract method or class
abstract void intake(); there should not be any body defined or even {} for ab
stract method since it is unknown.
if you define the method then it is no more abstract method.
if a class has abstract method then class also need to be defined as a abstrac
t class.
if a class is abstract then the class is incomplete
and so you cannot have instance or object for the abstract class, you can not
issue new
but you can have object variable for abstract class..
abstract class always derived/inherited and so you always have sub/child class
for abstract class.
All the sub/child class inherited from abstract class should have definition
for abstract method.
if doesnot have definition for abstract method then sub/child class also shoul
d defined as abstract class.
upcasting: passing child class variable to parent parameter.
downcast: if you want to access the child class method from parent class varia
ble then you have downclass the parent variable is also called dynamic binding.
if (parentclass-variable instanceof childclass-name){
((childclass-name)parentclass-variable).childclass-method()
}

Potrebbero piacerti anche