top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is Multiple Inheritance? How Java 8 supports Multiple Inheritance?

+1 vote
434 views
What is Multiple Inheritance? How Java 8 supports Multiple Inheritance?
posted Sep 22, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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

Can we say that java supports multiple inheritance?

For interfaces, yes.

Default methods in Java 8 can be viewed as a form of multiple inheritance (except that attribute can not be inherited). Consider the example below, the Button class implements two interfaces - Clickable and Accessible.

default-methods-java-8

Each interface defines a default method. The Button class therefore can call methods from both interfaces. This is like a multiple inheritance.

interface Clickable{
    default void click(){
        System.out.println("click");
    }
}

interface Accessible{
    default void access(){
        System.out.println("access");
    }
}

public class Button implements Clickable, Accessible {

    public static void main(String[] args) {
        Button button = new Button();
        button.click();
        button.access();
    }
}

If both of the implemented interfaces define a default method with same method signature, then the implementation class does not know which default method to use. The implementation class should define explicitly specify which default method to use or define it's own one. In the example below, Clickable and Accessible both define the print() method. In the Button class, the print() method specifies the default method.

default-methods-java-8-same-name

interface Clickable{
    default void click(){
        System.out.println("click");
    }

    default void print(){
        System.out.println("Clickable");
    }
}

interface Accessible{
    default void access(){
        System.out.println("access");
    }

    default void print(){
        System.out.println("Accessible");
    }
}

public class Button implements Clickable, Accessible {

    public void print(){
        Clickable.super.print();
        Accessible.super.print();
    }

    public static void main(String[] args) {
        Button button = new Button();
        button.click();
        button.access();
        button.print();
    }
}

The main motivation behind default methods is that if at some point we need to add a method to an existing interface, we can add a method without changing the existing implementation classes. In this way, the interface is still compatible with older versions. This is a cool feature. However, we should remember the motivation of using Default Methods and should keep the separation of interface and implementation.

answer Oct 9, 2017 by Manikandan J
...