CodesJava

Easy learning with example program codes

Why multiple inheritance is not supported java


Multiple inheritance is not supported by Java because of ambiguity problem. Let us consider the below example. We have two classes Test1 and Test2 which have same method show(). If multiple inheritance is possible than Test class can inherit properties and behaviour of both Test1 and Test2 classes. Now Test class have two show() methods inherited from Test1 and Test2. Problem occurs now in method call, when show() method is called with Test class object which method will be called, of Test1 class or Test2 class. That is why multiple inheritance is not supported in java.

Example:

/**
 * This is used to show that multiple inheritance
 * is not supported in java in case of classes.
 * @author CodesJava
 */
class Test1{
      public void show(){
            System.out.println("show details.");
      }
}
 
class Test2{
      public void show(){
            System.out.println("show details.");
      }
}
 
//let multiple inheritance is possible.
public class Test extends Test1, Test2 {
      public static void main(String args[]){
            Test obj = new Test();
            //Ambiguity problem in method call
            //which class show() method will be called.
            obj.show();
      }
}

Output:

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

Java nterview questions on inheritance

Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki