Why multiple inheritance is not supported java

In java, multiple inheritance is not supported because of ambiguity problem. We can take the below example where we have two classes Class1 and Class2 which have same method display(). If multiple inheritance is possible than Test class can inherit data members (properties) and methods (behaviour) of both Class1 and Class2 classes. Now, Test class have two display() methods inherited from Class1 and Class2. Problem occurs in method call, when display() method will be called with Test class object which method will be called, will it be of Class1 or Class2. This is ambiguity problem because of which multiple inheritance is not supported in java. Example:

class Class1{
      public void display(){
            System.out.println("Display method inside Class1.");
      }
}
 
class Class2{
      public void display(){
            System.out.println("Display method inside Class2.");
      }
}
 
//let multiple inheritance is possible.
public class Test extends Class1, Class2{
      public static void main(String args[]){
            Test obj = new Test();
            //Ambiguity problem in method call which class display() method will be called.
            obj.display();
      }
}

Output:

Exception in thread "main" java.lang.Error: 
Unresolved compilation problem:
at com.w3spoint.business.Test.main(Test.java:19)

Java interview questions on Inheritance

Please follow and like us:
Content Protection by DMCA.com