in Core Java

Multiple Inheritance – Why it is not part of Java?

Multiple Inheritance – Why it is not part of JAVA?

Multiple Inheritance – Sounds to be more powerful feature and one of the frequently asked questions – "Why Multiple Inheritance is not part of Java?"

Inheritance as a feature in Object Oriented Programming plays a vital role in promoting the reusability and enables Polymorphism. All this is good when Inheritance is confined to Many to One( I mean many classes extending one super class). 

Multiple Inheritance

So, we know that in Java the extends keyword only allows to extend only one class when used with a concrete class( except when used with Interfaces).  With the Inheritance, all the members of the super class will be accessible via the objects of the subclass(Of course, access modifiers should allow).

Now, Let us take a situation where we have class A which has add() method in it. When class B, C, D and E extend the class A, all the subclass objects will have access to add() method. 

Any subclass can override the add() method if they think the solution is not apt for them. When we try to access the add method through an object, the latest implementation will be executed. 

Multiple Inheritance and issues

Lets say, the extends keyword allows us to extend more than one concrete class.  What happens in this case. 

Let me explain this with an example.

class A has a method called add()

class A{

    public int add(){

    }

}

class B extends A{

public int add(){

}

}

 

class C extends A{

 

public int add(){

}

}

class D extends B,C{

public static void main(String[] args){

D d = new D();

d.add(),

}

}

Both B and C classes override the add() method. Now, when D extends B and C and when we try to access the add() method using object d, either the method implementation from class B or from class C should be executed.

But JVM cannot decide which one is the best to execute.

This ability to inherit multiple classes will only create more complexity and confusion in the design rather to help make it simple.

Multiple Inheritance

If you look at the structure of the classes you can see that the multiple inheritance actually forms a Diamond structure. This is the reason why they call this as "Diamond of Death"

Java Creators decided not to include this ability in Java to avoid bad designs.

So, next time when some one asks "Why Multiple Inheritance is not part of Java?" – Now.. You know the answer.:)

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Related Posts

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...