hybrid inheritance in java with example program

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 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

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.

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.