How would you implement multiple inheritance in Java?

 

Multiple inheritance is the ability to inherit from multiple classes. Java does not have this capability.

However, C++ does support multiple inheritance. Syntactically, multiple inheritance in C++ would look like this, where class X derives from 3 classes – A, B, and C:

class X : public A, private B, public C { /* ... */ };

The designers of Java considered multiple inheritance to be too complex, and not in line with the goal of keeping Java simple.


What a Java class does have is the ability to implement multiple interfaces – which is considered a reasonable substitute for multiple inheritance, but without the added complexity. This is what a Java class that implements multiple interfaces would look like:

// Wolf and Canine are interfaces

public class Dog implements Wolf, Canine {
/*...*/
}

So, remember that Java does not have multiple inheritance, but a Java class does have the ability to implement multiple interfaces.