A hybrid inheritance is a combination of more than one types of inheritance. For example when class A and B extends class C & another class D extends class A then this is a hybrid inheritance, because it is a combination of single and hierarchical inheritance. Let me show you this diagrammatically:
C ↑ | --------------- ↑ ↑ | | A B ↑ | D
If you want to learn the basics of inheritance, refer this guide: java inheritance
Hybrid Inheritance in Java
It seems that because of this diagram people are finding it difficult to understand this topic because this diagram shows combination of hierarchical and multiple inheritance and multiple inheritance is not supported in java.
The diagram is just for the representation, since multiple inheritance is not possible in java, It is not correct to show that as a part of hybrid inheritance. I will update the diagram whenever I get the time. You can refer the example that I have given at the beginning of post representing combination of single and hierarchical inheritance
Hybrid Inheritance Example
Lets write this in a program to understand how this works:
C ↑ | --------------- ↑ ↑ | | A B ↑ | D
Program: This example is just to demonstrate the hybrid inheritance in Java. Although this example is meaningless, you would be able to see that how we have implemented two types of inheritance(single and hierarchical) together to form hybrid inheritance.
Class A and B extends class C → Hierarchical inheritance
Class D extends class A → Single inheritance
class C { public void disp() { System.out.println("C"); } } class A extends C { public void disp() { System.out.println("A"); } } class B extends C { public void disp() { System.out.println("B"); } } class D extends A { public void disp() { System.out.println("D"); } public static void main(String args[]){ D obj = new D(); obj.disp(); } }
Output:
D
Harsh says
Is it possible that a class(C) extends a class (B) and implements an interface(A) as well?
something like : class c extends B ,implements A{}
Ram Narayan says
Yes Possible …
class XYZ extends ABC implements DEF
{
// code executes without error
}
Anuj says
In fact here in implemention you can implement more than 1 interface , as in you can implement as many as you want ,but extends only one class .
Superman says
Hello Harsh,
Class can’t extend interface so it’s not possible.
Have a good day.
Gagandeep Singh says
Where do u see a class extending any interface .. ?
techykudos says
Why not allowed abstract classes instead of interfaces in same approach? In that approach also we can implement the abstract method to avoid compiler confusion.
kshitij says
If we have to implement all the methods inherited then what is the use of inheriting them anyway? I don’t see code reusability
Sanyam Trehan says
These are just small examples to understand the concept of inheritance. When working on a live project like when implementing thread or runnable interfaces then code reusability is used.
Jeya Chitra says
Is it possible to combine single and hierarchical inheritance to form hybrid inheritance…….. If possible please give me a solution.
Gaurav Kumar Garg says
Hello Jeya Chitra,
It is possible, you can combine single inheritance and hierarchical inheritance.
I am giving one example where test5, test6 class extending class test4, Then class test7 is extending test5.
First we are applying hierarchical inheritance than we are implementing single inheritance.
Please check below example and try to understand how it is possible.
// Example of MultiLevel Inheritance
class test4
{
public void check()
{
System.out.println(“Calling In Test2”);
}
}
class test5 extends test4
{
public void check2()
{
System.out.println(“Calling In Test3”);
}
}
class test6 extends test4
{
public void check()
{
System.out.println(“Calling In Test2”);
}
}
class test7 extends test5
{
public void check()
{
System.out.println(“Calling In Test2”);
}
}
class hybrid_inherit extends test3
{
/**
Calling test1 & test2 class method from Multiple inherit class creating object.
*/
public static void main(String…args){
multi_lvl_inherit sh = new multi_lvl_inherit();
sh.check2();
sh.check();
}
}
Chaitanya Singh says
Nice Example Gaurav. Thanks for posting it here.
Ram Narayan says
“class B and C both are extending class A and they both have overridden the methodA(), which they can do as they have extended the class A. But since both have different version of methodA(), compiler is confused which one to call when there has been a call made to methodA() in child class D (child of both B and C, it’s object is allowed to call their methods), this is a ambiguous situation and to avoid it, such kind of scenarios are not allowed in java.”
sir, here i have a doubt … you said that we are overriding the same method in both child class of A(i.e. B and C).so it’s not allowing to extend both the classes in D. all good !!!
what if we are not overriding any value but want to use some other method from B and C class(no overriding)..say
class B
{
public void methodinB()
{
syso(“i am in B”);
}
}
public class c
{
public void methodinC()
{
syso(“i am in C”)
}
}
public class D extends B,C
{
// This is also not allowed here, but we are not overriding here.. Why is this not allowed ???
}
Chaitanya Singh says
Dear Ram Narayan, The program you have mentioned would throw error because multiple inheritance is not allowed in java and I have already explained why its not allowed.
Sachin says
Brother hybrid inheritance is a combination of two type inheritances it is not necessary to take a single and a multiple inheritance we can also take other things like single and Hierarchical inheritance etc. So Java does support Hybrid inheritance but there is a fact that there should not be a case where two classes are having the same child class.if I am wrong please let me know.
Chaitanya Singh says
Absolutely correct Sachin, I think you got confused because of the diagram I have shown here. The diagram is just for the representation, since multiple inheritance is not possible in java. It is not correct to show that as a part of hybrid inheritance. I will update the diagram whenever I get the time. I will also post this as a note in the above post so that others don’t get confused. Thanks for taking the time to post a comment here.