inheritance
TRANSCRIPT
OpenLab[[email protected]] 2
Define
● Deriving new classes from existing classes such that the new classes acquire all the features of existing classes is called inheritance
OpenLab[[email protected]] 3
How
● Use of keyword extends
A
B<extends>
class A{
- - - - - -- - -
}
class B extends A{
- - - - - -- - -
}
Super Class
Sup Class
OpenLab[[email protected]] 4
Types of Inheritance in Java
● Multilevel Inheritance
AB
● Simple Inheritance
C
A
B C. . .
OpenLab[[email protected]] 5
Why ????
● Why NO to multiple Inheritance in Java ?
Dimond Problem A
B C
D
OpenLab[[email protected]] 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[[email protected]] 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[[email protected]] 9
A
B
B b new B( )=
Reference of Sub Class
Object of Sub Class
<extends>
SUB:SUB
<access>
<access>
OpenLab[[email protected]] 10
A
B
A a new A( )=
Reference of SUPER Class
Object of SUPER Class
<extends>
SUPER:SUPER
<access>
OpenLab[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 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[[email protected]] 20
A
B
A a new B( )=
Reference of SUPER Class
Object of SUB Class
<extends>
Exclusive:Methods[Widening/Generalization]
OpenLab[[email protected]] 21
A
B
A a new B( )=
Reference of SUPER Class
Object of SUB Class
<extends>
Override:Methods[Widening/Generalization]
OpenLab[[email protected]] 22
A
B
B b new A( )=
Reference of Sup Class
Object of Super Class
<extends>
Exclusive:Methods[Narrowing:Specialization]
OpenLab[[email protected]] 23
A
BA aB b
new B( )(B) a
=
Reference of SUPER Class
Object of Super Class
<extends>
Exclusive:Methods[Narrowing:Specialization]
=
OpenLab[[email protected]] 24
A
BA aB b
new B( )(B) a
=
Reference of SUPER Class
Object of Super Class
<extends>
Override:Methods[Narrowing:Specialization]
=
OpenLab[[email protected]] 25
Type Casting Referenced Data Type
ParentGeneralization/Downcasting
Specialization/Upcasting
* study in detail after inheritence