Inheritance
OpenLab[openlabword@gmail.com] 2
Define
● Deriving new classes from existing classes
such that the new classes acquire all the
features of existing classes is called
inheritance
OpenLab[openlabword@gmail.com] 3
How
● Use of keyword extends
A
B
<extends>
class A
{
- - -
- - -
- - -
}
class B extends A
{
- - -
- - -
- - -
}
Super Class
Sup Class
OpenLab[openlabword@gmail.com] 4
Types of Inheritance in Java
● Multilevel Inheritance
A
B
● Simple Inheritance
C
A
B C. . .
OpenLab[openlabword@gmail.com] 5
Why ????
● Why NO to multiple Inheritance in Java ?
Dimond Problem A
B C
D
OpenLab[openlabword@gmail.com] 6
Multiple Inheritance
A
B C
OpenLab[openlabword@gmail.com] 7
NOTE
“When SUB Class object is created , first of all
the super class default constructor is called and
then only SUB class constructor is called”
OpenLab[openlabword@gmail.com] 8
From SubClass To SuperClass
● Private Members not accessible from
SubClass
● From SubClass object we can access the
SuperClass methods and properties
● From SuperClass object we can not access
the SubClass methods and properties
OpenLab[openlabword@gmail.com] 9
A
B
B b new B( )=
Reference of Sub Class
Object of Sub Class
<extends>
SUB:SUB
<access>
<access>
OpenLab[openlabword@gmail.com] 10
A
B
A a new A( )=
Reference of SUPER Class
Object of SUPER Class
<extends>
SUPER:SUPER
<access>
OpenLab[openlabword@gmail.com] 11
Protected Specifier
● It is commonly used in SUPER class to make
the member of the SUPER class available
directly in its SUB class.
OpenLab[openlabword@gmail.com] 12
Overriding
● An instance method in a SUB class with the
same signature and return type as an
instance method in the SUPER class
overrides the superclass's method
OpenLab[openlabword@gmail.com] 13
SUB:SUB
[Method:Override]
A
B
B b new B( )=
Reference of Sub Class
Object of Sub Class
<extends>
<access>
<CAT>
<access>
<CAT>
OpenLab[openlabword@gmail.com] 14
Rules for Overriding
● Argument and return type must be same
● A final method can not be overridden
● A method declared static cannot be override
● If a method cannot be inherited , then it cannot
be override
● Constructors cannot be override
OpenLab[openlabword@gmail.com] 15
Run Time Polymorphism
● It is also called as dynamic polymorphism
A
B
C
A a
A a
A a
= new A( )
= new B( )
= new C( )
a.
i)
ii)
iii)
OpenLab[openlabword@gmail.com] 16
Overloading vs Overriding
● Overloading
– Static/Compile time
Polymorphism
– JVM decides which
method is called
depending on the
difference in the
method signature
– It is done in same class
– Method return type can
be same or different
● Overriding
– Dynamic/Run Time
Polymorphism
– JVM decides which
method is called
depending on the data
type of the object used to
calls
– It is done in SUPER and
SUB class
– Method return type
should also be same
OpenLab[openlabword@gmail.com] 17
super
● From SUB class object we can access the
super class methods and properties
● Use of super :-
– If we have same names of member in SUB as in
SUPER class then SUB members are accessible
– Invoke SUPER class constructor
OpenLab[openlabword@gmail.com] 18
final
● Final Methods
– Methods declared final are called final methods
– NO to overriding : As : NOT available to SUB
class
– When we wish that its implementation should not be
changed
● Final Class
– NO to SUB class : NO to inheritance
OpenLab[openlabword@gmail.com] 19
Type Casting
OpenLab[openlabword@gmail.com] 20
A
B
A a new B( )=
Reference of SUPER Class
Object of SUB Class
<extends>
Exclusive:Methods
[Widening/Generalization]
OpenLab[openlabword@gmail.com] 21
A
B
A a new B( )=
Reference of SUPER Class
Object of SUB Class
<extends>
Override:Methods
[Widening/Generalization]
OpenLab[openlabword@gmail.com] 22
A
B
B b new A( )=
Reference of Sup Class
Object of Super Class
<extends>
Exclusive:Methods
[Narrowing:Specialization]
OpenLab[openlabword@gmail.com] 23
A
B
A a
B b
new B( )
(B) a
=
Reference of SUPER Class
Object of Super Class
<extends>
Exclusive:Methods
[Narrowing:Specialization]
=
OpenLab[openlabword@gmail.com] 24
A
B
A a
B b
new B( )
(B) a
=
Reference of SUPER Class
Object of Super Class
<extends>
Override:Methods
[Narrowing:Specialization]
=
OpenLab[openlabword@gmail.com] 25
Type Casting
Referenced Data Type
ParentGeneralization
/Downcasting
Specialization
/Upcasting
* study in detail after inheritence

Inheritance