Multiple Inheritance in Java 8 through Interface

Multiple Inheritance in Java is nothing but one class extending more than one class. Previous versions of Java(until JDk 7) doesn’t support Multiple Inheritance because it causes a famous problem called Diamond Problem and hence indirectly Multiple Inheritance in Java is achieved using Interfaces. After the introduction of Default Methods in Java 8, even the interfaces can also have the method bodies. In this tutorial let’s see how the Diamond Problem is prevented in Java 8.

What is Diamond Problem in Java ?

Let’s first see What is Diamond Problem in Java and Why Java doesn’t support Multiple Inheritance

Multple Inheritance Diamond Problem

Multiple Inheritance in Java Example

class A
{
    public void disp()
    {
        System.out.println("A disp() method called");
    }
}
class B extends A
{
    @Override
    public void disp()
    {
        System.out.println("B disp() method called");
    }
}
class C extends A
{
    @Override
    public void disp()
    {
        System.out.println("C disp() method called");
    }
}
public class D extends B,C //Not Supported by Java
{
   public static void main(String args[])
   {
       D d = new D();
       d.disp(); // Ambiguity which disp() to call
   }
}
  • Class B and Class C inherits Class A and the disp() method of Class A is overridden by both B and C
  • Class D inherits both Class B and C (Not Supported by Java), If suppose we need to call the disp() method through the instance of Class D, then the Java compiler will not know which method to call whether disp() method of Class B or Class C. It results in ambiguity
  • In order to overcome the above issue, Multiple Inheritance is achieved through Interface. As in Java we can implement more than one java interface.

Multiple Inheritance in Java 8

In Java 8 Interface can also have method definition using Default Methods, then obviously it should also result in ambiguity isn’t it ? Yes, but Java 8 can handle this type of compatible issues. Lets look into the below example.

package com.javainterviewpoint;

interface Car
{
    public default void drive()
    {
        System.out.println("Car is driving");
    }
}
interface Jeep
{
    public default void drive()
    {
        System.out.println("Jeep is driving");
    }
}
public class Vehicle implements Car,Jeep
{

    public static void main(String args[])
    {
        Vehicle v = new Vehicle();
        v.drive();
                
    }
}

when we try to execute above class, we will be getting the “Unresolved compilation problem”

Multiple Inheritance in Java 8

Explicit Overriding :

In Java 8 you cannot implement multiple interfaces having same signature, without explicitly overriding the methods in the child class.

interface Car
{
    public default void drive()
    {
        System.out.println("Car is driving");
    }
}
interface Jeep
{
    public default void drive()
    {
        System.out.println("Jeep is driving");
    }
}
public class Vehicle implements Car,Jeep
{
    @Override
    public void drive()
    {
        System.out.println("Vehicle is driving");
    }
    public static void main(String args[])
    {
        Vehicle v = new Vehicle();
        v.drive();
    }
}

Output :
Vehicle is driving

Using Super Keyword

It is also possible to call the Parent interface method explicitly from the Child class. Lets take the above scenario if we want to call the Car interface drive() method, we can do that with the help of Super Keyword.

interface Car
{
    public default void drive()
    {
        System.out.println("Car is driving");
    }
}
interface Jeep
{
    public default void drive()
    {
        System.out.println("Jeep is driving");
    }
}
public class Vehicle implements Car,Jeep
{
    @Override
    public void drive()
    {
        Car.super.drive();
    }
    public static void main(String args[])
    {
        Vehicle v = new Vehicle();
        v.drive();
    }
}

Output :
Car is driving

Leave a Reply

Your email address will not be published. Required fields are marked *