Multiple Inheritance in Java

Multiple Inheritance in Java

Multiple inheritance is the feature wherein one subclass can inherit from more than one class. In simpler words, a class can have more than one superclass. Java does not support multiple inheritance when it is a key feature of object oriented programming. The reason for this is that it leads to a lot of confusion when both the subclass as well as all the superclasses have the same method name and signature. During method call, it becomes very difficult for the compiler to determine which method is to be called. Even if a method is called, the compiler finds it all the more difficult to determine the priority of the method calls. Let us look at the following example below. This code will give an error in Java:

class Parent1
{
void fun ()
{
System.out.println (“Parent1”);
}
}

class Parent2
{
void fun ()
{
System.out.println (“Parent 2”);
}
}

class Test extends Parent1, Parent 2
{
public static void main (String args [])
{
Test t = new Test ();
t.fun();
}
}

 

OUTPUT

Compiler Error

In the code above, when fun() is being called by the Test class, the compiler does not know whether to invoke the fun() method of Parent1 class or the fun() method of Parent2 class.

The Diamond Problem

The Diamond Problem is generally referred to as the sequence wherein one subclass extends from two superclasses. Both the superclasses, in turn, inherit from another class. This sequence again leads to the problem of multiple inheritance. The following code, when executed in Java, gives a compilation error.

class GrandParent
{
void fun ()
{
System.out.println (“Grandparent”);
}
}

class Parent1 extends GrandParent
{
void fun ()
{
System.out.println (“Parent1”);
}
}

class Parent2 extends GrandParent
{
void fun ()
{
System.out.println (“Grandparent”);
}
}

class Test extends Parent1, Parent2
{
public static void main (String args [])
{
Test t = new Test ();
t.fun ();
}
}

 

OUTPUT

Error:
prog.java.31: error: ‘{’ expected
class Test extends Parent1, Parent2
1 error

In this code also, the problem arises where the compiler is confused about which fun () method to call. The grandparent class has a fun method, and both the parent classes also have fun methods. To avoid such conflicts, Java does not allow multiple inheritance.

Simplicity

Java avoids multiple inheritance to keep the functionality of the language as simple and easy to comprehend as possible. Multiple inheritance brings with it computational complexity. This creates problems during various operations like casting, constructor chaining, etc. It has also been observed that programs that involve multiple inheritance can be alternately coded without implementing such complexity. Therefore, considering all circumstances and cases, Java has left out this feature.

Default Methods and Interfaces

Java 8 has introduced default methods. This functionality allows interfaces to provide default implementation to methods. Java allows one class to implement more than one interface. It can happen that two interfaces have the same default method containing the same name and parameter list. In such cases, the implementing class has to properly define which default method it wants to call or override the default method.

Sample Code

The following code demonstrates multiple inheritance through default methods.

interface PI1
{
default void show ()
{
System.out.println (“Default PI1”);
}

interface PI2
{
default void show ()
{{
System.out.println (“Default PI2”);
}
}

class TestClass implements PI1, PI2
{
public void show ()
{
PI1.super.show ();
PI2.super.show ();
}

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

 

OUTPUT

Default PI1

Default PI2

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.