Java does not support Multiple and Multipath inheritance


Reading time: 30 minutes | Coding time: 10 minutes

Inheritance is an useful OOP techniques but if used incorrectly, can result in several runtime errors as in case with C++. To make things easy, Java sets some hard limitations on inheritance and due to which not all inheritance tricks are available which effectively eliminates most of the possible errors.

In this article, we will look at the limitation set by Java on inheritance and how it cannot support Multiple and Multipath inheritance.

We will have a basic class with a single method as follows:

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}

Similarly, we will have multiple classes to stimulate situations.

Limitation of inheritance in Java

The only limitation in Java is that we cannot inherited multiple classes at a time. For example, the following code tries to inherit class A and B from C which is invalid:

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}
class B
{
   public void methodB()
   {
      System.out.println("method of Class B");
   }
}
class C extends A, B
{
  public void methodC()
  {
     System.out.println("method of Class C");
  }
}
class JavaExample
{
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     //All classes can access the method of class A
     obj1.methodB();
     obj2.methodA();
  }
}

Note this line:

class C extends A, B

It will throw the following compile time error:

prog.java:15: error: '{' expected
class C extends A, B
                 ^
1 error

You may wonder why is this? as in C and C++, we can inherit from multiple classes comfortably.

The problem in inheriting from multiple classes is that:

  • If a particular member variable and function are same in the multiple classes we inherit, it will be a problem in runtime to determine function of which parent class has been refered.

This has been a major issue and been the source of several runtime errors in C++. To tackle this, C++ has two solutions:

  • use class scope
  • inherit using a virtual keyword

Java takes a different approach. To avoid the turnarounds involved in the process, Java simply does not allow users to get into this situation by raising a compilation error. It has been proven to be an effective approach and does not bring in any significant implementation limitation.

This is certainly one of the points why C++ code cannot be coverted into Java code with a simple parsing techniques without recreating the program flow.

In terms of inheritance types that uses the fact of inheriting multiple classes, we can two types:

  • Multiple inheritance
  • Multipath inheritance

Both are not allowed in Java.

Multiple inheritance

Multiple inheritance is a type of inheritance which involves 3 classes as follows:

  • class A
  • class B
  • class C inherits class A and B

Due to the last step (class C inheriting class A and B), we cannot implement this in Java.

Example implementation (causes error):

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}
class B
{
   public void methodB()
   {
      System.out.println("method of Class B");
   }
}
class C extends A, B
{
  public void methodC()
  {
     System.out.println("method of Class C");
  }
}
class JavaExample
{
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     //All classes can access the method of class A
     obj1.methodB();
     obj2.methodA();
  }
}

Error:


prog.java:15: error: '{' expected
class C extends A, B
                 ^
1 error

Multipath inheritance

Multipath inheritance is a type of inheritance that involves 4 classes as follows:

  • class A
  • class B inherits class A
  • class C inherits class A
  • class D inherits class B and C

Due to the last step (class D inheriting class B and C), we cannot implement this in Java.

Implementation (causes error):

class A
{
   public void methodA()
   {
      System.out.println("method of Class A");
   }
}
class B extends A
{
   public void methodB()
   {
      System.out.println("method of Class B");
   }
}
class C extends A
{
  public void methodC()
  {
     System.out.println("method of Class C");
  }
}
class D extends A, B 
{
    public void methodD()
    {
        System.out.println("method of Class C");
    }
}
class JavaExample
{
  public static void main(String args[])
  {
     B obj1 = new B();
     C obj2 = new C();
     //All classes can access the method of class A
     obj1.methodB();
     obj2.methodA();
  }
}

Error:

prog.java:22: error: '{' expected
class D extends A, B 
                 ^
1 error

Impact of this

We are not able to support two common types of inheritance in Java but this is for good as:

  • Eliminates possible errors due to un-attended edge cases
  • Coversion from C++ to Java will require control flow logic to handle these cases