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.

4
  • you can't have implementation in interfaces unless you are using JVM with java 7 and the method is declared static Commented Feb 24, 2017 at 13:42
  • 2
    Possible duplicate of Are defaults in JDK 8 a form of multiple inheritance in Java? Commented Feb 24, 2017 at 13:51
  • @DanyalSandeelo I have Java 8 and this code is correct and it work Commented Feb 24, 2017 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 is a short article about it. Commented Feb 24, 2017 at 14:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
I wouldn't use the word multiple inheritance and java in same sentence
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.
0

Java doesn't support multiple inheritance.

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

1 Comment

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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.