Multiple Inheritance Troubles

Java-Multiple Inheritance Troubles

Multiple Inheritance Troubles

In many real-life situations, we wish to define a class in such a way that it inherits the resources of several distinct classes. This type of inheritance is technically known as multiple inheritance. Java does not support this feature. Now that an interface can include default methods, you might be wondering if an interface can provide a way around this restriction. The answer is, essentially, no. Default methods do offer a bit of what one would normally associate with the concept of multiple inheritance. For example, you might have a class that implements two interfaces. If each of these interfaces provides default methods, then some behavior is inherited from both. Thus, to a limited extent, default methods do support multiple inheritance of behavior. As you might guess, in such a situation, it is possible that a name conflict will occur.

For example, assume that two interfaces called DataOne and DataTwo are implemented by a class called MyData. What happens if both DataOne and DataTwo provide a method called data( ) for which both declare a default implementation? Is the version by DataOne or the version by DataTwo used by MyData? Or, consider a situation in which DataTwo extends DataOne. Which version of the default method is used? Or, what if MyData provides its own implementation of the method? To handle these and other similar types of situations, Java defines a set of rules that resolves such conflicts. First, in all cases, a class implementation takes priority over an interface default implementation. Thus, if MyData provides an override of the data( ) default method, MyData’ version is used. This is the case even if MyData implements both DataOne and DataTwo. In this case, both defaults are overridden by MyData’ implementation.

Second, in cases in which a class implements two interfaces that both have the same default method, but the class does not override that method, then an error will result. Continuing with the example, if MyData implements both DataOne and DataTwo, but does not override data( ), then an error will occur.

Multiple Inheritance Troubles

In cases in which one interface inherits another, with both defining a common default method, the inheriting interface’s version of the method takes precedence. Therefore, continuing the example, if DataTwo extends DataOne, then DataTwo’s version of data( ) will be used. It is possible to explicitly refer to a default implementation in an inherited interface by using super keyword. For example, if DataTwo wants to refer to DataOne’s default for data( ), it can use the statement “DataOne.super.data();”.

Multiple Inheritance Troubles

Program Source

interface DataOne {
    
    default void data() {
        
        System.out.println("DataOne");
    }
    
}
interface DataTwo extends DataOne {
    
    default void data() {
        
        DataOne.super.data();
        System.out.println("DataTwo");
    }
}
class MyData implements DataTwo {

    public void data(){
        
        DataTwo.super.data();
    }
}
class Javaapp {
    
    public static void main(String[] args) {
        
        MyData my = new MyData();
        my.data();
    }
}

Leave a Comment