Multiple Inheritance in Java

As we have long learned the fact that multiple inheritance is not directly supported in Java, well that was only till Java 7. In Java 8, we can realize the concept of multiple inheritance through use of default methods without getting into diamond problem.

Let’s see how?

Table of Contents

1. What are default methods?
2. How multiple inheritance is achieved via default methods?
3. Possible conflicts and their resolutions

1. What are default methods?

If you been in java programming since enough long time, you may realize that how painful can be adding a new method in an existing interface. You will need to implement that new method in java classes which implement that interface. It’s really difficult job. Well, java 8 brought default methods to help you in exactly same situation.

Default methods enable you to add new functionality to the interfaces and ensure backward compatibility for existing classes which implement that interface.

As their name implies, default methods in interfaces are methods which will be invoked by default – if not overridden in implementing classes. Let’s understand with an example.

Moveable interface is some existing interface and wants to add a new method moveFast(). If it adds moveFast() method using old technique, then all classes implemeting Moveable will also be changed. So, let’s add moveFast() method as default method.

public interface Moveable 
{
    default void moveFast()
    {
        System.out.println("I am moving fast, buddy !!");
    }
}

If all classes implementing Moveable interface do not need change themselves (until some class specifically wants to override moveFast() method to add custom logic). All classes can directly call instance.moveFast() method.

public class Animal implements Moveable
{
    public static void main(String[] args)
    {
        Animal tiger = new Animal();

        //Call default method using instance reference
        tiger.moveFast();
    }
}

2. How multiple inheritance is achieved via default methods?

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and behavior from more than one parent object or parent class.

We know that in java (until jdk 7), inheritence in java was supported by extends keyword which is used to create a child class from a parent class. You cannot extend from two classes.

Until java 7, interfaces were only for declaring the contracts which implementing classes MUST implement (except the implementing class in not abstract itself). So there was no specific behavior attached with interfaces which a class can inherit. So, even after a class was capable of implementing as many interfaces as it want, it was not appropriate to term as multiple inheritance.

But since java 8’s default methods, interfaces have behavior as well. So now if a class implement two interfaces and both defines default methods, then it is essentially inheriting behaviors from two parents which is multiple inheritance.

For example, in below code Animal class does not define any of it’s own behavior; rather it is inheriting behavior from parent interfaces. That’s multiple inheritance.

package com.howtodoinjava.examples;

interface Moveable
{
    default void moveFast(){
        System.out.println("I am moving fast, buddy !!");
    }
}
 
interface Crawlable
{
    default void crawl(){
        System.out.println("I am crawling !!");
    }
}
 
public class Animal implements Moveable, Crawlable 
{
    public static void main(String[] args) 
    {
        Animal self = new Animal();
        
        self.moveFast();
        self.crawl();
    }
}

3. Possible conflicts during mutiple inheritance

In above example, we have two different interfaces and two different methods – so there is no conflict. What if both interfaces decide to define a new method with same name. Well they can define without problem. But which method will be invoked when Animal instance will call it’s name.

That’s conflicting situation.

package com.howtodoinjava.examples;

interface Moveable
{
    default void run(){
        System.out.println("I am running, kid !!");
    }
}
 
interface Crawlable
{
    default void run(){
        System.out.println("I am running, daddy !!");
    }
}
 
public class Animal implements Moveable, Crawlable 
{
    public static void main(String[] args) 
    {
        Animal self = new Animal();

        //What will happen when below statement will execute
        //self.run();
    }
}

So solve above conflict, caller class must decide which run() method it want to invoke and then call using interface’s reference like below.

Moveable.super.run();   //Call Moveable's run() method

//or 

Crawlable.super.run();  //Call Crawlable's run() method

That’s all you should know about multiple inheritance feature Java 8, using default methods.

Happy Learning !!

Leave a Reply

10 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.