0% found this document useful (0 votes)
43 views37 pages

OOP 3 Principles - Inheritance Polymorphism Encapsulation Abstraction

Uploaded by

nirajks3103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views37 pages

OOP 3 Principles - Inheritance Polymorphism Encapsulation Abstraction

Uploaded by

nirajks3103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

OOP 3 : Principles -

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.

💡 Child class will have the properties of the Base class


and it will also have some additional properties of it’s
own.

To inherit a class, you simply incorporate the definition of one


class into another by using the extends keyword.

class Child extends Parent


{
// body of class
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 1


Types of Inheritance
1. Single Inheritance → One Class extends another class

2. Multilevel Inheritance → A → B → C Here C is the subclass of B


and B is a subclass of A, so C is also a subclass of A

3. Multiple Inheritance → When one Child Class has more than


one Parent Classes. NOT ALLOWED IN JAVA. Can be achieved
using Interfaces

4. Heirarchical Inheritance →One Parent Class has more than one


Child Classes

5. Hybrid Inheritance → Combination of → { Single + Multiple }


Inheritance. NOT ALLOWED IN JAVA. Can be achieved using
Interfaces

1. Single Inheritance
One Class extends another class

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 2


You can only specify one superclass(Parent class) for any
subclass(Child class) that you create. Java does not support the
inheritance of multiple superclasses into a single subclass. You
can, as stated, create a hierarchy of inheritance in which a
subclass becomes a superclass of another subclass. However, no
class can be a superclass of itself.
Example of inheritance

public class Parent


{
int length;
int height;
int width;

Parent()
{
this.height=1;
this.length=0;
this.width=0;
}
}

public class Child extends Parent


{
int weight;

public Child()
{
this.weight=4;
}
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 3


public class Main
{
public static void main(String[] args)
{

Child kid = new Child();

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.height=69; // Just as we access the members o


kid.length=68;
kid.width=96;

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
}
}

💡 But, this is Not Okay because we have not yet specified


the Base Class’(Parent) properties(length, width, height)
inside the Derived Class(Child).We should do it, by
passing all the Base class members in a constructor in
the Child class.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 4


Suppose we have parent class “Species” and it has 2 child
classes → “Human” and “Fish”. Now, lets say the parent class
“Species” has a property call “age”. So both the child classes
will inherit the “age” property from “Species”

So whenever we try to create a new object for class “Human” →


Human niraj and we try to access the “age” variable →
= new Human( );

niraj . age , it will be fetched from the parent class “Species”


because any variable called “age” does NOT EXIST in the class
“Human” but it is present in it’s parent class “Species”, so it
can use it’s properties.

💡 But, If we are trying to access some variables that are


in the Parent class of the Child class, then we need to
initialize those variables in the Child class too.

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.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 5


💡 So, whenever we call a constructor like this Human

, we have to initialize the variables of the


new Human( );
niraj =

Parent class also.

// Box Class-------------------------------- has properties -> l


public class Box
{
int l;
int w;
int h;

Box()
{
this.l=0;
this.w=0;
this.h=0;
}

//CUBE
Box(int side)
{
this.l=side;
this.w=side;
this.h=side;
}

//When we pass our own Dimensions


Box(int l, int w, int h)
{
this.l=l;
this.w=w;

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 6


this.h=h;
}

//When we pass an entire Box


Box(Box old)
{
this.l=old.l;
this.w=old.w;
this.h=old.h;
}

public void information()


{
System.out.println("Running the Box");
}
}

// BoxWeight Class -> Child Class of Box Class--------Inhertits

public class BoxWeight extends Box


{
int weight;

BoxWeight()
{
this.weight=-1;
}

public BoxWeight(int l, int w, int h, int weight)


{
super(l, w, h);
this.weight = weight;
}
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 7


// Main Class with psvm() function.

public class Main


{
public static void main(String[] args)
{
BoxWeight box1 = new BoxWeight(); // This Constructor wh

System.out.println(box1.h); /* height can be

System.out.println(box1.l); // Similary , prints defaul


System.out.println(box1.w); // Similary , prints defaul

System.out.println(box1.weight); // This is a property o

BoxWeight box2 = new BoxWeight(4, 6, 10, 25); /* This "b

/* super(l, w, h) -> This is used to initialize vlaues


3 properties/varibles l, w, h i.e. -> Box(int l,

System.out.println(box2.h);
System.out.println(box2.l);
System.out.println(box2.w);
System.out.println(box2.weight);
}
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 8


private

💡 Although Child Class/Subclass can access the


properties/variable/members of the Parent
Class/Superclass, it will not be able to access the
properties/variable/members of the Parent Class that have
been declared as “Private”

For eg. → If I declare variable l in Box Class as Private, then


i wont’t be able to access it in the BoxWeight Class( Child
class of Box ), but i can access it and modify it inside the Box
Class itself.

public class Box


{
private int l;
int w;
int h;

Box(int l, int w, int h)


{
this.l=l;
this.w=w;
this.h=h;
}
}

public class BoxWeight extends Box


{
int weight;

BoxWeight()

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 9


{
this.weight=-1;

this.l=2000; // ERROR . Can't use l varibale in the B


}

public BoxWeight(int l, int w, int h, int weight)


{
super(l, w, h); // But why does this NOT GIVE AN ERR
/* Because BoxWeight Class is not initializing l(priv
i.e. -> Box(int l, int w, int h) */

this.weight = weight;
}
}

public class Main


{
public static void main(String[] args)
{
BoxWeight box1 = new BoxWeight();

System.out.println(box1.l); // This also Gives a


}
}

💡 A Child Class can access the variables of Parent Class


unless they are declared as “Private”, but a Parent Class
can’t access the variables/members that are specific to
Child Class

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 10


💡 So if we create an object specifically of Parent Class
and then try to use it to access the members specific to
the Chils Class, then we can’t do that. Gives an ERROR.

For eg.→

public class Main


{
public static void main(String[] args)
{
Box box1 = new Box(); // Parent Class Objec
BoxWeight box2 = new BoxWeight(); // Child Class Object

System.out.println(box1.weight); /* This also Gi

System.out.println(box2.weight); /* This works b


}
}

But, now let’s suppose what will happen if we create a refernce


variable of type Parent Class(Box) but we create the object of
the Child Class(BoxWeight). Something like this→

Box box3 = new BoxWeight( 1, 2, 3, 8 ) ; where l=1, h=2, w=3, weight=8.


So now, what will happen if we try to access

1. members of Parent Class → Box

2. members of Child Class → BoxWeight

public class Main


{
public static void main(String[] args)

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 11


{
Box box3 = new BoxWeight( 1, 2, 3, 8); // l=1, h=2, w=3
/* Here the reference variable "box3" is of typ

// 1. Trying to access the members of Box(Parent class)


System.out.println(box3.l); // Prints -> 1
System.out.println(box3.h); // Prints -> 2
System.out.println(box3.w); // Prints -> 3

// 2. Trying to access the members of BoxWeight(Child class)


System.out.println(box3.weight); // Gives an ER

/* Because , the reference variable "box3" is of type Box(Paren


of the Child Class of type BoxWeight */

// When a reference to a subclass object is assigned to a superc

}
}

💡 It is actually the type of reference variable and NOT the


type of the object that determines what members can be
accessed

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 12


💡 In the above example the the reference variable → “ box3 ”
is of type Box(Parent Class) and the object → new BoxWeight( 1,
is of type BoxWeight(Child Class). And we know
2, 3, 8)

that the type of reference variable determines which


members will be accessed. And since the reference
variable → “ box3 ” is of type Box i.e. Parent Class so
this refernce variable cannot access the members of
BoxWeight because we know that Parent Class cannot access
the members of it’s Child Class.

🔥 A Superclass Variable Can Reference a Subclass Object:

🔥 When a reference to a subclass object is assigned to a


superclass reference variable, you will have access only
to those parts of the object defined by the superclass.

Now, what will happen if have the reference variable of type


Child Class and the object of type Parent Class. Something like
this →

BoxWeight box4 = new Box( 1, 2, 3, 8 ) ; where l=1, h=2, w=3, weight=8. →


Given an ERROR

public class Main


{
public static void main(String[] args)
{
BoxWeight box4 = new Box( 1, 2, 3, 8); // l=1, h=2, w=3
/* Here the reference variable "box4" is of typ

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 13


/*
EXPLANATION :-
# There are many variables in both Parent and Child Classes
# You are given access to variables that are in the reference ty
# Hence, You should have access to "weight" variable
# This also means that the members/variables you are trying to a
# But here, when the object itself is of type Parent Class(Box),
# We are not able to initialize the variable "weight"(member of
# And Parent class(Box) has no idea of the members (weight) of t
This is why ERROR
*/

// Box Class has no idea of it's Child Class -> BoxWeight. But C

}
}

🔥 So, you can’t have a reference variable of type Child


Class and the object of type Parent Class.

super keyword
Whenever a Sub-Class(Child Class) needs to call a Super-
Class(Parent Class) constructor, we use the super keyword.

🔥 In Java an “Object Class” is a Super-Class to all the


classes - the predefined ones like Arrays Class etc. and
even the Classes that user creates. All classes in Java
are sub-classes to “Object Class”

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 14


🔥 super keyword allows the Child Class to access properties
of the Directly above Parent Class. For eg.→ if we have a
heirarchy like A→B→C, then C’s directly above Parent
Class is C

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.

public class Box


{
private int l;
int w;
int h;
int weight=100;

Box(int l, int w, int h)


{
this.l=l;
this.w=w;
this.h=h;
}
}

public class BoxWeight extends Box

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 15


{
int weight;

BoxWeight()
{
this.weight=-1;
}

public BoxWeight(int l, int w, int h, int weight)


{
super(l, w, h);

System.out.println(this.w); // prints-> 6 => v


System.out.println(super.w); // prints-> 6 =>

System.out.println(this.weight); // this value o


System.out.println(super.weight); // this value

this.weight = weight;
}
}

public class Main


{
public static void main(String[] args)
{
BoxWeight box1 = new BoxWeight();
BoxWeight box2 = new BoxWeight(4, 6, 10, 69);
}
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 16


2. Also,

Because the Super Class But, the subclass (Child


doesn’t know/care about the class) cares about what
SubClass and it’s variables, superclass(Parent Class)
so it initializes itself contains i.e. what variables
first. does the superclass has, so

🔥 If we don’t use super(l, w, h); inside the Child Class’


constructor i.e BoxWeight(int l, int w, int h, int weight) , then
instead of calling the parameterized Parent Class’
constructor i.e. Box(int l, int w, int h) it calls the default
Parent Class’ constructor → Box() , because we are
initializing the variables by calling the Child Class
constructor , but inside the Child Class constructor we
are not using the super(l, w, h); to initialize the
variables of the Parent Class( Box ) i.e. l, w, h.

3. Using super to access an entire different BoxWeight type


object passed as a parameter in the BoxWeight constructor→
BoxWeight(BoxWeight other) and using the “super” inside the

BoxWeight constructor to initialize the varaibles of the


Box(Parent Class) class → super(other)
Explanation :- It is same as the reason we saw, when we did
“It is actually the type of
BoxWeight box4 = new Box( 1, 2, 3, 8); ⇒
reference variable and NOT the type of the object that

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 17


determines what members can be accessed” and we know that
this object “other” is of type BoxWeight which is Child Class
of Box(Parent Class). Hence, it can access the variables of
the Box(Parent Class) Class.

// Box Class (Parent Class)


public class Box
{
int l;
int w;
int h;

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;
}

//When we pass our own Dimensions


Box(int l, int w, int h)
{
this.l=l;
this.w=w;
this.h=h;
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 18


//When we pass an entire Box
Box(Box old) // --> COPY constructor of Parent
{
this.l=old.l;
this.w=old.w;
this.h=old.h;
}

public void information()


{
System.out.println("Running the Box");
}
}

// BoxWeight Class(Child Class)

public class BoxWeight extends Box


{
int weight;

BoxWeight()
{
this.weight=-1;
}

public BoxWeight(int l, int w, int h, int weight)


{
super(l, w, h);
this.weight = weight;
}

BoxWeight(BoxWeight other) // COPY constructor of Chil

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 19


{
super(other); // Doesn't Give an ERROR //--> Cal
this.weight=other.weight;
}
}

/* # In BoxWeight COPY constructor -> super(other), here "o


# In the Box Class the COPY constructor " Box( Box old)
# So, "old" of Box(Parent) type is being assigned "o
# Does BoxWeight type has access to members of Box t
# This whole scenario can be seen as Box old = new BoxWe
# and we know that “It is actually the type of refer
that determines what members can be accessed”
# So, the reference type -> "old" is of Box(Parent)
# In the COPY constructor of Box(Box old) will not h
when BoxWeight(BoxWeight other) constructor is c

/* How the control of the code follows in this :-

# We pass the object "other" of BoxWeight type as a paramete


# The COPY constructor of Child Class i.e. BoxWeight is invo
# Inside this COPY constructor of Child Class the "weight" v
# To initialize the rest of the variables i.e. l, w, h that
# This invokes the COPY constructor of the Parent Class Box
# In this scenario the object we passed "other" is of type B
# We know that “It is actually the type of reference variabl
# So, "old" initializes the variables l,w,h but it can't ini
# "old" has access only to l, w, h.

And we know that Box old = new BoxWeight(other) is similar


/* Because, the reference variable "old" is of type Box(Par
But we cannot access "weight" because it belongs to the

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 20


already initialized by the BoxWeight COPY constructo

/* And the reason why "old" can't access weight because


/* When a reference to a subclass object is assigned to a su
you will have access only to those parts of the object

/* as we already know that= “It is actually the type of refe

2. Multilevel Inheritance
When a class extends a class that extends another class. A → B → C

Here C is the subclass of B and B is a subclass of A, so C is


also a subclass of A.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 21


🔥 In an empty constructor of a Child Class → BoxWeight() ,
even if we don’t specifically write/include super() to get
the default values of the members/variables( l=0, w=0,
h=0) of the Parent Class (Box), Java automatically
assigns/includes it( super() ) itself in the code that
calls the empty constructor of the Parent Class → Box()
that assignes the default values of l, w, h.

🔥 But, in case of passing the parameters from Child class


to Parent Class’s parameterized Constructor super(l,w,h)
must be written explicitly in the the parameterized
constructor of the Child Class → BoxWeight( l, w, h, weight )
otherwise the compiler will automatically invoke the
empty constructor of the parent class → Box() by using
super()

3. Multiple Inheritance
When one Class extends to more than one Classes

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 22


Now, lets suppose :

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 23


This creates Ambiguity , not able to decide which one to pick

🔥⇒ Hence, JAVA does not allow/support Multiple Inheritance


because of Ambiguity.

🔥 If two or more Parent classes have the same Child class


and if both the Parent Classes have a same
member/variable , then the Child class will get confused
on which Parent classes property are we trying to access.

💡 Multiple Inheritance can lead to or cause “Diamond


Problem”

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 24


How can we implement Multiple Inheritance
in Java :———————
⇒ Using Interfaces ( 👈🏻 Click It )

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.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 25


public class BoxWeight
{
// all variables & methods
}

public class BoxPrice extends BoxWeight


{
// all variables & methods
}

public class BoxColor extends BoxWeight


{
// all variables & methods
}

/* Here BoxWeight is the Parent Class and BoxPrice & BoxColor ar

5. Hybrid Inheritance
Combination of → { Single + Multiple } Inheritance.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 26


🔥 Since, in Java, we don’t have Multiple Inheritance, so
Java also doesn’t have Hybrid Inheritance

How can we implement Hybrid Inheritance in


Java :———————
⇒ Using Interfaces ( 👈🏻 Click It )

Polymorphism
Poly→ Many , Morphism→ Ways to represent

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 27


PolyMorphism→ Many ways to represent a single entity.

// ##### Parent Class --> Shapes


public class Shapes
{
void area()
{
System.out.println("I'm in Shapes");
}

// ##### Child Class 1 --> Square


public class Square extends Shapes
{

void area()
{
System.out.println("Area is sq. of sides");
}

// ##### Child Class 2 --> Circle


public class Circle extends Shapes
{

void area()
{
System.out.println("Area is pi*r*r ");
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 28


}

// #### Main Class

public class Main


{
public static void main(String[] args)
{
Shapes shape = new Shapes();
Circle circle = new Circle();
Square square = new Square();

square.area(); // prints --> "Area is sq. of side


circle.area(); // prints --> "Area is pi*r*r "
shape.area(); // prints --> "I'm in Shapes"

Shapes squareShape = new Square(); // Here refere

squareShape.area(); // prints --> "Area is sq. of

/* Since reference variable is of type Shapes(Parent Class) and


not the type of object, so it should be printing "I'm in

}
}

⇒ We’ll see how it works, but first we need to know a few


things.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 29


Types of Polymorphism
1. Compile-Time/Static Polymorphism → Achieved via Method
Overloading
Method Overloading : When methods/functions have the same
name but different types of argument, different number of
argument, different return types and ordering of the type of
parameters should be different. For eg. → Multiple
Constructors ⇒
A obj = new A(); , A obj2 = new A(3 , 4); , A obj3= new

A(name, 5) etc.

2. Run-Time/Dynammic Polymorphism → Achieved by Method


Overriding

Method Overriding : If subclass (child class) has the same


method as declared in the parent class, it is known as method
overriding in Java.

// ##### Child Class 2 --> Circle


public class Circle extends Shapes
{

// This will run when object of Circle is created. Hence it is O

@Override // --> This can be written above any method t


void area()
{
System.out.println("Area is pi*r*r ");
}
}

But, In main method when we

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 30


// #### Main Class

public class Main


{
public static void main(String[] args)
{

Shapes squareShape = new Square(); // Here refere

squareShape.area(); // prints --> "Area is sq. of

/* This is because the object is of type Square class, so it inv

/* But, earlier we hav learnt that what members can be accessed

/****** How OVERRIDING works ********/

Parent obj = new Child(); // Suppose Parent & Child both have

/* Here, which method Class' method "print()", Parent's or Child

// But, How JAVA dtermines this, that which mehtod, Parent's or

How JAVA determines which method to run

public class Parent


{
void print()
{
System.out.println("This is Parent Class");
}
}

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 31


public class Child extends Parent
{
void print()
{
System.out.println("Child Class is here");
}
}

public class Main


{
public static void main(String[] args)
{
Parent obj = new Child(); // Here reference va
obj.print(); // prints --> "Child Class is h

/* And it is true that.. (i) "It is the type reference Variabl


But, (ii) "It is the type

/* So, For method overriding to be able to work in this example,


Because if Parent class doesn't have "print()" method , then
is of type Parent Class, so it can only access members of it
And now, when the reference variable 'obj' is able to access "pr
that is of type Child class, determines that the "print()" metho

In Simpler terms --> Parent obj = new Child();


Here 'obj' is of Parent type so, it specifies that it can only a
Now, the new object created is of Child Class type because of th
Parent and Child Class have "print()" method, but since object i

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 32


🔥 Selection of which method to use/call is done at Run-
Time.

🔥 If we use the “final” keyword to make a method/function


as final, then we cannot override it.

🔥 “final” can also be used to prevent inheritance. If we


make the Parent class as “final”, then it can’t be
inherited by other Child classes.

🔥 If we declare a Class as “final”, then implicitly all


it’s members and methods become “final”.

Why “final” methods cannot be overridden.

Can static methods be overridden


⇒ No.
🔥 static methods can be inherited.

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 33


🔥 static methods cannot be overridden.

ANS: because the method is static. It does not get


Overridden due to the properties of static itself.
⇒ static members doesn’t depend on objects, so the
original static method in the Parent class will always be
called , not the one in the Child Class no matter which
class’s object we use to reference /call it either parent
or child class object. And since static methods can be
inherited, so we can even use the child class’ object to
referernce it, it will still call the original static
method that is in the Parent class.

// Parent class --> Box

public class Box


{
static void greeting()
{
System.out.println("This is Box class, Greeting"
}
}

// Child class --> BoxWeight


public class BoxWeight extends Box
{
static void greeting()
{
System.out.println("BoxWeight class here , Greet
}
}

public class Main

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 34


{
public static void main(String[] args)
{

Box box1 = new Box();


box1.greeting(); // prints --> "This is Box clas
Box.greeting(); // prints --> "This is Box class

// Now if we try to override and get the BoxWeight class greetin

Box box2 = new BoxWeight();


box2.greeting(); // prints --> "This is Box clas

// Also, if we do this, here even the reference variable is of t


BoxWeight box3 = new BoxWeight();
box3.greeting(); // prints --> "This is Box clas

// Also,
BoxWeight.greeting(); // prints --> "This is Box

/* Explanation : We are still able to get the same result or we


because "static methods can be inherited" , but "static
Parent class' static method will be called.

/* Even if we remove the greeting() method from BoxWeight(Child


original static method from the Parent class. -> Because

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 35


🔥 💡💡💡 Overriding depends on Objects, static members
doesn’t depend on objects.
Hence, static methods cannot be overridden. 💡💡💡

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.

This is sort of a security feature.


We can achieve this via packages and access modifiers.

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.

Abstraction vs. Encapsulation

OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 36


OOP 3 : Principles - Inheritance, Polymorphism, Encapsulation, Abstraction 37

You might also like