loading...
Cover image for Multiple Inheritance in Java

Multiple Inheritance in Java

kuljeet profile image kuljeet singh ・4 min read

This article will help you implement a concept that is otherwise not possible to implement in Java. I am referring to Multiple Inheritance In Java. Following points will be touched upon in this article

Mutiple Inheritance

Multiple inheritance a feature of some object-oriented programming languages in which a class or an object inherits characteristics and properties from more than one parent class or object. This is contrary to the single inheritance property, which allows an object or class to inherit from one specific object or class.

Alt Text

 // First Parent class
class ParentClass1
{
    void show()
    {
        System.out.println("ParentClass1");
    }
}

// Second Parent Class
class ParentClass2
{
    void show()
    {
        System.out.println("ParentClass2");
    }
}
class SubClass extends ParentClass1, ParentClass2{
 public static void main(String[] args) {
  SubClass obj = new SubClass();

  obj.show();
 }
}

Output:

SubClass.java:18: error: '{' expected
class SubClass extends ParentClass1, ParentClass2{
                                   ^
1 error

Java and Multiple Inheritance
Object Oriented Programming provides a user the feature of multiple inheritance, wherein a class can inherit the properties of more than a single parent class. In simpler terms, multiple inheritance means a class extending more than one class.

Multiple inheritance in java means one class implementing two or more than two interfaces simultaneously. In simple words multiple inheritance in java means one class accessing the states and behavior of two or more than two interfaces simultaneously. Multiple inheritance is shown below.
Alt Text

Why Java doesn’t support multiple inheritance?
C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem that occurs in multiple inheritance.
There are 2 reasons mentioned that will give you a idea why we don’t have multiple inheritance in java.
1.The Diamond Problem
2.Simplicity

  • What is diamond problem ?

So the 1st is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

          A foo()
        /      \
      /          \
   B foo()     C foo()
      \          /
        \       /
         D foo()

Even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

Alt Text

In this image, Class A has a method named foo().
Class B and Class C inherits from Class A and ==overrides== the implementation of foo() in their own way.
But when Class D inherits from Class B and Class C, this leads to a confusion which ==overridden== implementation to use in Class D. Whether it should be Class B: foo() or Class C: foo().

  • Simplicity- Multiple inheritance is not supported by Java using classes , handling the complexity that causes due to multiple inheritance is very complex. It creates problem during various operations like casting, constructor chaining etc and the above all reason is that there are very few scenarios on which we actually need multiple inheritance, so better to omit it for keeping the things simple and straightforward. ## Alternative to multiple inheritance

Although, multiple inheritance is no more a part of Java but still, there is a way we can implement the same along with resolving the ambiguity of the above-explained problem.
The solution to the problem is interfaces.
The only way to implement multiple inheritance is to implement multiple interfaces in a class.
In java, one class can implements two or more interfaces. This also does not cause any ambiguity because all methods declared in interfaces are implemented in class.

Alt Text

Note:-An interface contains variables and methods like a class but the methods in an interface are abstract by default unlike a class.

Implementation:

interface Interface1
{
   public void show();
}
interface Interface2
{
   public void show();
}
class SubClass implements Interface1, Interface2
{
   public void show()
   {
       System.out.println("A class can implements more than one interfaces");
   }
   public static void main(String args[]){
    SubClass obj = new SubClass();
    obj.show();
   }
}

Output:

A class can implements more than one interfaces

Example 1

This is the basic Example through which we are going to implement the Multiple Inheritance using ==Interfaces==. The Main method implements both the interfaces i.e. InterfaceOne and InterfaceTwo. It executes without any ambiguity.

Code:

interface MyInterface1 {
    void m();
}

interface MyInterface2 {
    void n();
}


/*
 * Class implements two interfaces and provides implementation to the
 * methods of both interface.
 */
public class MyClass implements MyInterface1, MyInterface2{
    public static void main(String[] args) {
           MyClass obj=new MyClass();
           obj.m();
           obj.n();          
    }

    @Override
    public void m(){
           System.out.println("in implementation of MyInterface1's m()");
    }

    @Override
    public void n(){
           System.out.println("in implementation of MyInterface2's n()");
    }
}

Output:

in implementation of MyInterface1's m()
in implementation of MyInterface2's n()

Example 2

This is the one more example through which we are going to implement the Multiple Inheritance in Java using ==Interfaces==.

Code:

interface Writeable
{
    void writes();
}
interface Readable 
{   
    void reads();

}
class Student implements Readable,Writable
{
    public void reads()
    {
    stem.out.print(“Student reads.. ”);
    }
    public void writes()
    {
    System.out.print(“ Student writes..”);
    }

    public static void main(String args[])
    {
    Student s = new Student();
    s.reads();
    s.writes();
    }
}

Output:

Student reads..
Student writes..

Conclusion
Thus, multiple inheritance can be achieved by the methods discussed in this article.

Posted on by:

kuljeet profile

kuljeet singh

@kuljeet

Hii! My name is Kuljeet and I'm a Full Stack web developer with a passion for front end development, design and back end development. I'm currently a final year student at GGSIPU.

Discussion

pic
Editor guide