Multiple Inheritance in Java
Tags: java, inheritance, programming
Published: 2020-08-23 07:49:22

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](https://dev-to-uploads.s3.amazonaws.com/i/5h9kgtlok2ntvvvq5gp4.png)
 ```
 // 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.<br><br>
    
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](https://dev-to-uploads.s3.amazonaws.com/i/p608uehve4i8kq8r2yx5.png)

 **Why Java doesn’t support multiple inheritance?**
     C++ , Common lisp and few other languages supports multiple inheritance</b> while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the <b>ambiguity</b> caused by it. One of the example of such problem is the <b>diamond problem</b> 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](https://dev-to-uploads.s3.amazonaws.com/i/5sjw0rkhyi8zkct4ltge.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/n8jlpn3bkcdq7k2ptom8.jpg)

**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.