Why multiple inheritance is not supported in Java

JavaJava Programming Java 8

In Java, a class cannot extend more than one class. Therefore following is illegal −

Example

public class extends Animal, Mammal{}

However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritances.

The reason behind this is to prevent ambiguity.

Consider a case where class B extends class A and Class C and both class A and C have the same method display().

Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.

raja
Published on 02-Feb-2018 09:41:54
Advertisements