Search code examples
javainheritanceinterfacemultiple-inheritance

java and multiple inheritance


Can we call this code as multiple inheritance ?

interface Interface {

    public int alpha = 0;
    public int calculA(int a, int b);
    public int calculB(int a, int b);
}

interface InterfaceA extends Interface {

    public default int calculA(int a, int b) {
        return a + b;
    }
}

interface InterfaceB extends Interface {

    public default int calculB(int a, int b) {
        return a - b;
    }
}

class TestInterface implements InterfaceA, InterfaceB {

    public TestInterface() {
        System.out.println(alpha);
        System.out.println(calculA(5, 2));
        System.out.println(calculB(5, 2));
    }

    public static void main(String[] args) {
        new TestInterface();
    }
}

It look like the default keyword permit to have a multiple inheritance.

It is correct or this concept have an another name ?

Thank's

Edit

It's not a duplicate of Are defaults in JDK 8 a form of multiple inheritance in Java? because this thread talking about the feature called Virtual Extensions.

My question is to ask if my implementation is called multiple inheritance or something else.


Solution

  • java doesn't supports multiple inheritance.

    What you are doing is implementing an interface. You can't extend multiple classes in java but you can implement multiple interfaces.

    An interface is a reference type and it is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. An interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.

    A class describes the attributes and behaviors of an object and an interface contains behaviors that a class implements.

    For more on interface, click here