1

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.

Community
  • 1
  • 1
MonkeyJLuffy
  • 195
  • 3
  • 15
  • you can't have implementation in interfaces unless you are using JVM with java 7 and the method is declared static – Danyal Sandeelo Feb 24 '17 at 13:42
  • 2
    Possible duplicate of [Are defaults in JDK 8 a form of multiple inheritance in Java?](http://stackoverflow.com/questions/7857832/are-defaults-in-jdk-8-a-form-of-multiple-inheritance-in-java) – Calculator Feb 24 '17 at 13:51
  • @DanyalSandeelo I have Java 8 and this code is correct and it work – MonkeyJLuffy Feb 24 '17 at 13:51
  • 1
    There are different kinds of multiple inheritance. Your code features multiple inheritance of types and behavior (through virtual extension methods) not multiple inheritance of state (which is not possible in Java). [Here](http://www.lambdafaq.org/do-default-methods-introduce-multiple-inheritance-to-java/) is a short article about it. – Calculator Feb 24 '17 at 14:55

2 Answers2

1

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

Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • Thank's, I know that. But now, it looks like using default methods in Interface like I do before, permit to have a kind of multiple inheritance. My question is just it is correct to name it like this or differently. – MonkeyJLuffy Feb 24 '17 at 14:07
  • I wouldn't use the word `multiple inheritance` and `java` in same sentence – Yousaf Feb 24 '17 at 14:11
  • I know all that concepts. And it's why I ask my question here to confirm or not. I know that one of the first Law Of Java is to not have multiple inheritance, but it look like, by my implementation, it's now possible. – MonkeyJLuffy Feb 24 '17 at 14:14
0

Java doesn't support multiple inheritance.

What you have currently done is implement multiple interfaces which is absolutely permittable.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • Right, it is permitable, and it is permitable to implement by default this methods, then you could make multiple inheritance by this way. It looks like at least... – MonkeyJLuffy Feb 24 '17 at 14:17