03 Inheritance
03 Inheritance
03 Inheritance
INHERITANCE.
Inheritance
Types of Inheritance, single Inheritance, multilevel Inheritance, Hierarchical Inheritance.
Method & Constructor Overloading & Overriding,
Dynamic Method Dispatch
Final variables, final methods, final class.
Use of super keyword
Abstract methods & classes,
Static members and Methods.
Reusability of code is the most important aspect of OOP paradigm. It is always best practice to reuse something that
already exists rather than creating the same all over again.
INHERITANCE
Inheritance is the mechanism of deriving new class from old class. The old class is knows as superclass or
base class or parent class.The new class is known as subclass or derived class or child class.
The subclass inherits all of its instances variables and methods defined by the superclass and Sub class also
adds its own unique elements like instances variables and methods.
Thus we can say that, subclass are specialized version of superclass.
Rules about Inheritance.
Inheritance and different visibility modifires (private/public/protected/friendly)
o All public variables of Super class will be inherited by subclass.
o Private member can not be inherited by subclass because it will not be visible to subclass.
o Protected members of Super class will be inherited by subclass as protected. So these mebers can
directly visible in Subclass methods but not accessible by object of Subclass.
o All default variables (friendly) will be inherited by all subclass.
Inheritance and Constructor
o Constructor is not inherited by Sub class.
o The default constructor of Super Class is autometically invoked when Sub Class object is created.
o The parameterised constructor of Super class is invoked from the Sub class constructor using super()
method.
Inheritance and Package
o All default variables (friendly) will be inherited by all subclass in the same package only. Subclass
outside the package will not inherit any default memeber.
o Protected variable will be inherited by all subclass in the same package or outside package. (Different
from default).
Hirarchical Inheritance :
One Class is extended by many Subclasses. It is One-to-many relationship.
Hirarchical inheritance is that, in which there is only one super class for multiple subclasses.
It is the mechanism of deriving a multiple subclasss from the Superclass.
All the properties of superclass are extended to all subclasss except private properties.
The Sub classes also have its own data members ,methods and also have methods inherited from super class.
Syntax of Hirarchical Inheritance is as follow.
class SuperClass
{
// Instance variable(s) declaration;
//Constructor(s) defination;
//Instance method(s) defination;
}
class SubClass_1 extends SuperClass
{
//Instance variable(s) declaration of SubClass_1;
//Constructor(s) defination of SubClass_1;
//Instance method(s) defination of SubClass_1;
}
class SubClass_2 extends SuperClass
{
//Instance variable(s) declaration of
SubClass_2;
//Constructor(s) defination of SubClass_2;
//Instance method(s) defination of SubClass_2;
}
Multilevel Inheritance:
One Class is extended by Subclass_1 and Subclass_1 is extended by Subclass_2 and
so on.
Multilevel inheritance is that, in which Super class A is extended by Subclass B and
then further Subclass B is (Super Class for C) extended by Subclass C. Then this whole
structure is called as Multilevel Inheritance.
It is ladder or hierarchy of single level inheritance.
Multiple Classes are involved in this inheritance, but one class extends by only one
class.
The lowermost Subclass can make use of all it super classes members.
All the properties of superclass A are extended to B, and properties of class B (including inherited from class A)
are extended to class C except private properties of A and B.
Each Sub classes also have its own data members, methods and also have methods inherited from its super
class.
Syntax of Multilevel Inheritance is as follow.
class SuperClass
{
// Instance variable(s) declaration;
//Constructor(s) defination;
//Instance method(s) defination;
}
class SubClass_1 extends SuperClass
{
//Instance variable(s) declaration of SubClass_1;
//Constructor(s) defination of SubClass_1;
//Instance method(s) defination of SubClass_1;
}
class DMDDemo
{
public static void main( String args[] )
{
SuperClass A = new SuperClass(); //Super class object is created
SubClass B = new SubClass(); //Sub class object is created
SuperClass C; //Super class Referance is created
C=A; //Now Referance C contains object of Super Class
C.sum(10,20); //Super class method Dynamically Executed
C=B; //Now Referance C contains object of Sub Class
C.sum(10,20); //Sub class method Dynamically Executed
}
}