Due to diamond problem java doesn’t support multiple inheritance.
1 2 3 4 5 6 7 |
GrandParent / \ / \ Parent1 Parent2 \ / \ / Test |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// A Grand parent class in diamond class GrandParent { void fun() { System.out.println("Grandparent"); } } // First Parent class class Parent1 extends GrandParent { void fun() { System.out.println("Parent1"); } } // Second Parent Class class Parent2 extends GrandParent { void fun() { System.out.println("Parent2"); } } // Error : Test is inheriting from multiple // classes class Test extends Parent1, Parent2 { public static void main(String args[]) { Test t = new Test(); t.fun(); } } |
Multiple inheritance is not supported by Java using classes , handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there are very few scenarios on which we actually need multiple inheritance, so better to omit it for keeping the things simple and straightforward.