Below are the different types of inheritance which is supported by Java.
- Single Inheritance
- Multiple Inheritance (Through Interface)
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance (Through Interface)
Lets see about each one of them one by one.
1. Single Inheritance in Java
Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.
Single Inheritance Example
public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } public static void main(String args[]) { //Assigning ClassB object to ClassB reference ClassB b = new ClassB(); //call dispA() method of ClassA b.dispA(); //call dispB() method of ClassB b.dispB(); } }
Output :
disp() method of ClassA disp() method of ClassB
2. Multiple Inheritance in Java
Multiple Inheritance is nothing but one class extending more than one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces.
3. Multilevel Inheritance in Java
In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.
MultiLevel Inheritance Example
public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassB { public void dispC() { System.out.println("disp() method of ClassC"); } public static void main(String args[]) { //Assigning ClassC object to ClassC reference ClassC c = new ClassC(); //call dispA() method of ClassA c.dispA(); //call dispB() method of ClassB c.dispB(); //call dispC() method of ClassC c.dispC(); } }
Output :
disp() method of ClassA disp() method of ClassB disp() method of ClassC
4. Hierarchical Inheritance in Java
In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent class for ClassB, ClassC and ClassD.
Hierarchical Inheritance Example
public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassA { public void dispC() { System.out.println("disp() method of ClassC"); } } public class ClassD extends ClassA { public void dispD() { System.out.println("disp() method of ClassD"); } } public class HierarchicalInheritanceTest { public static void main(String args[]) { //Assigning ClassB object to ClassB reference ClassB b = new ClassB(); //call dispB() method of ClassB b.dispB(); //call dispA() method of ClassA b.dispA(); //Assigning ClassC object to ClassC reference ClassC c = new ClassC(); //call dispC() method of ClassC c.dispC(); //call dispA() method of ClassA c.dispA(); //Assigning ClassD object to ClassD reference ClassD d = new ClassD(); //call dispD() method of ClassD d.dispD(); //call dispA() method of ClassA d.dispA(); } }
Output :
disp() method of ClassB disp() method of ClassA disp() method of ClassC disp() method of ClassA disp() method of ClassD disp() method of ClassA
5. Hybrid Inheritance in Java
Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting as Parent for ClassD.
Hope you have got a better understanding towards the different types of inheritance in Java. Happy Learning 🙂
Sairam says
nice
kuber says
usefull for examination
Shivani says
Thank you……………nicely briefly explained
Asim says
Nice
siva says
Thnk u 4 ur information…….. It’s useful 4 me
MUKESH says
AWESOME
Sony kashyap says
Good material for easy self learning
muhammad daniyal says
thanks!!
kirankumar says
good
Sirichandana says
Nice…good explanation.tnqq
It is very useful
jiya says
thank you
munisha says
awesome explanation
Marshal frank says
Very accurate results and vital one.
Neha says
Easy to understand and it helped me a lot…. thankyou
Sher Says says
awesome explanation
easy to understand
Marin says
It was really useful. Thnk u
Satpal sharma says
Good explain
Sharmin Nahar says
Thanks… ????
Anshul Chawla says
Appropriate material for last time revision during exams.
Ìñßæñê says
Well defined explanation.????
Sai Kumar Reddy says
That was amazing explanation thq so much
Hitendra says
Awesome explanation ….
Shanu says
Very useful. Thank u so much
Fakhre Alam says
Nicely Explained .
Thank you so much .