Does Java support multiple inheritance? Why? How can we resolve this?

Java 8Object Oriented ProgrammingProgramming

Whenever, you extend a class a copy of superclass’s members is available to the subclass object and, when you can call the method of the superclass using the object of the subclass.

Example

In the following example, we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).

Now, you create an object of the subclass and call the method demo().

 Live Demo

class SuperClass{
   public void demo() {
      System.out.println("demo method");
   }
}
public class SubClass extends SuperClass {
   public static void main(String args[]) {
      SubClass obj = new SubClass();
      obj.demo();
   }
}

Output

demo method

Multiple inheritance in java

For suppose, if we have two classes namely, SuperClass1 and SuperClass2 with the same method say demo() (including parameters) as shown below −

class SuperClass1{
   public void demo() {
      System.out.println("demo method");
   }
}
class SuperClass2{
   public void demo() {
      System.out.println("demo method");
   }
}

Now, from another class, if extend both the classes as −

public class SubClass extends SuperClass1, SuperClass2 {
   public static void main(String args[]) {
      SubClass obj = new SubClass();
      obj.demo();
   }
}

According to the basic rule of inheritance, a copy of both demo() methods should be created in the subclass object which leaves the subclass with two methods with the same prototype. Then, if you call the demo() method using the object of the subclass compiler faces an ambiguous situation not knowing which method to call.

Therefore, in Java multiple inheritance is not allowed and, you cannot extend more than one other class. Still, if you try to do so, a compile-time error is generated.

Compile time error

On compiling, the above program generates the following error −

MultipleInheritanceExample.java:9: error: '{' expected
public class MultipleInheritanceExample extends MyInterface1, MyInterface2{
                                                               ^
1 error

Multiple inheritance using interfaces

In case of multiple interfaces with the same default method. In the concrete class implementing both interfaces, you can implement the common method and call both super methods. thus You can achieve multiple inheritance in Java using interfaces.

Example

 Live Demo

interface MyInterface1{
   public static int num = 100;
   public default void display() {
      System.out.println("display method of MyInterface1");
   }
}
interface MyInterface2{
   public static int num = 1000;
   public default void display() {
      System.out.println("display method of MyInterface2");
   }
}

public class InterfaceExample implements MyInterface1, MyInterface2{
   public void display() {
      System.out.println("This is the implementation of the display method");
   }
   public void show() {
      MyInterface1.super.display();
      MyInterface2.super.display();
   }

   public static void main(String args[]) {
      InterfaceExample obj = new InterfaceExample();
      obj.show();
   }
}

Output

display method of MyInterface1
display method of MyInterface2
raja
Published on 05-Jul-2019 15:56:58
Advertisements