Types of Inheritance in Java – Single, Multiple, Multilevel, Hierarchical & Hybrid Below are the different types of inheritance which are supported by Java. Single Inheritance Multiple Inheritance (Through Interface) Multilevel Inheritance Hierarchical Inheritance Hybrid Inheritance (Through Interface) Let's 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 Subclass and Class A will be one and only Superclass.Single_Inheritance_in_Java 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 inheritances in Java using Interfaces. Multiple_Inheritance_in_Java 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 subclasses. 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 🙂 - Study24x7
Social learning Network
study24x7

Default error msg

Login

New to Study24x7 ? Join Now
Already have an account? Login
86 followers study24x7 20 Mar 2019 03:30 PM study24x7 study24x7

Types of Inheritance in Java – Single, Multiple, Multilevel, Hierarchical & Hybrid Below are the different types of inheritance which are supported by Java.
Single Inheritance Multiple Inheritance (Through Interface) Multilevel Inheritance Hierarchical Inheritance Hybri...

See more

Types of Inheritance in Java – Single, Multiple, Mu...
study24x7
Write a comment
Related Questions
500+   more Questions to answer
Most Related Articles