Hybrid Inheritance is a combination of both Single Inheritance and Multiple Inheritance. Since in Java Multiple Inheritance is not supported directly we can achieve Hybrid inheritance also through Interfaces only.
As we can see in the above diagram ClassA is the Parent for both ClassB and ClassC which is Single Inheritance and again ClassB and ClassC again act as Parent for ClassC(Multiple Inheritance which is not supported by Java). Lets now see below code on what will happen if we go for implementing Hybrid Inheritance with both classes and interfaces.
Implementation of Hybrid Inheritance with Classes
public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void show() { System.out.println("show() method of ClassB"); } public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassA { public void show() { System.out.println("show() method of ClassC"); } public void dispC() { System.out.println("disp() method of ClassC"); } } public class ClassD extends ClassB,ClassC { public void dispD() { System.out.println("disp() method of ClassD"); } public static void main(String args[]) { ClassD d = new ClassD(); d.dispD(); d.show();//Confusion happens here which show method to call } }
Output :
Error!!
We all know that we cannot extend ClassB and ClassC to ClassD at the same time but the point to be noted here is why it is not allowed. Since we have same show() method in both ClassB and ClassD compiler will not be able to differentiate which method to call whereas if we are using interface we can avoid that ambiguity.
Implementation of Hybrid Inheritance with Interfaces
public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public interface InterfaceB { public void show(); } public interface InterfaceC { public void show(); } public class ClassD extends ClassA implements InterfaceB,InterfaceC { public void show() { System.out.println("show() method implementation"); } public void dispD() { System.out.println("disp() method of ClassD"); } public static void main(String args[]) { ClassD d = new ClassD(); d.dispD(); d.show(); } }
Output :
disp() method of ClassD show() method implementation
As we can see in the above code the ClassD has implemented both the interfaces InterfaceB and InterfaceC. In this case we didn’t have ambiguity even though both the interfaces are having same method.
Priya says
Java that allows an object of a class to own the variables and methods of another class. Inheritance in Java is realized using the keyword extends. thanks for sharing.
Dimple says
Hi,
As we can see in the above code the ClassD has implemented both the interfaces InterfaceA and InterfaceB. In this case we didn’t have ambiguity even though both the interfaces are having same method.
As per programming Class D implements both Interface B and Interface C but in description written InterfaceA and Interface B
which one is correct?
javainterviewpoint says
Good catch.Corrected the typo