OOP 3 Principles - Inheritance Polymorphism Encapsulation Abstraction
OOP 3 Principles - Inheritance Polymorphism Encapsulation Abstraction
Inheritance, Polymorphism,
Encapsulation, Abstraction
Inheritance
Inheritance in Java is a mechanism in which one object acquires
all the properties and behaviors of a parent object. It is an
important part of OOPs.
The idea behind inheritance in Java is that you can create
new classes that are built upon existing classes. When you
inherit from an existing class, you can reuse methods and fields
of the parent class. Moreover, you can add new methods and
fields in your current class also.
1. Single Inheritance
One Class extends another class
Parent()
{
this.height=1;
this.length=0;
this.width=0;
}
}
public Child()
{
this.weight=4;
}
}
System.out.println(kid.height); // prints-> 1
System.out.println(kid.length); // prints-> 0
System.out.println(kid.width); // prints-> 0
System.out.println(kid.weight); // prints-> 4
kid.weight=100;
System.out.println(kid.height); // prints-> 69
System.out.println(kid.length); // prints-> 68
System.out.println(kid.width); // prints-> 96
System.out.println(kid.weight); // prints-> 100
}
}
Because in niraj . age , the “age” here i.e. the property “age” of
class “Human” is also specific to every single human(object) of
this “Human” Class.
Box()
{
this.l=0;
this.w=0;
this.h=0;
}
//CUBE
Box(int side)
{
this.l=side;
this.w=side;
this.h=side;
}
BoxWeight()
{
this.weight=-1;
}
System.out.println(box2.h);
System.out.println(box2.l);
System.out.println(box2.w);
System.out.println(box2.weight);
}
}
BoxWeight()
this.weight = weight;
}
}
For eg.→
}
}
// Box Class has no idea of it's Child Class -> BoxWeight. But C
}
}
super keyword
Whenever a Sub-Class(Child Class) needs to call a Super-
Class(Parent Class) constructor, we use the super keyword.
Uses:———————————
1. To access the members of the Super-Class(Parent Class). We
can also use “this” keyword, but →Suppose the Parent Class
has a variable named “weight” and the Child Class also has a
variable named “weight”. Now, inside the Child Class
constructor, if we try to access the “weight” variable of
Parent Class using “this” keyword then it would give us the
value of “weight” of the Child Class. Hence , it is good
practice to use super keyword to access the variables of the
superclass.
BoxWeight()
{
this.weight=-1;
}
this.weight = weight;
}
}
int weight=24;
Box()
{
this.l=0;
this.w=0;
this.h=0;
}
//CUBE
Box(int side)
{
this.l=side;
this.w=side;
this.h=side;
}
BoxWeight()
{
this.weight=-1;
}
2. Multilevel Inheritance
When a class extends a class that extends another class. A → B → C
3. Multiple Inheritance
When one Class extends to more than one Classes
4. Heirarchical Inheritance
One Parent Class has more than one Child Classes. One Class is
inherited by many Classes.
Similar to multiple Single Inheritance, all subclasses having a
single Parent class.
For eg.
5. Hybrid Inheritance
Combination of → { Single + Multiple } Inheritance.
Polymorphism
Poly→ Many , Morphism→ Ways to represent
void area()
{
System.out.println("Area is sq. of sides");
}
void area()
{
System.out.println("Area is pi*r*r ");
}
}
}
A(name, 5) etc.
Parent obj = new Child(); // Suppose Parent & Child both have
// Also,
BoxWeight.greeting(); // prints --> "This is Box
Encapsulation
Wrapping up the implementation of the data
members(variables/methods) in the Class.
It means to restrict accessing the properties of a class to the
outside world.
Abstraction
Hiding unnecessary details and showing only valuable
information.
For eg. If we have a car and we have a key and we use it to
start the car. Now we don’t need to know the internal mechanics
of how the car starts→ how ignition happens, how petrol is
pumped, how combustion happens. No. We don’t care about that
stuff, we just care and need to know that we put in the key and
the car starts, that’s it.