Why Is There No Multiple Inheritance in Java, But Implementing Multiple Interfaces Is Allowed

Why is there no multiple inheritance in Java, but implementing multiple interfaces is allowed?

Because interfaces specify only what the class is doing, not how it is doing it.

The problem with multiple inheritance is that two classes may define different ways of doing the same thing, and the subclass can't choose which one to pick.

Why does Java not allow multiple inheritance but does allow conforming to multiple interfaces with default implementations

Things are not so simple.

If a class implements multiple interfaces that defines default methods with the same signature the compiler will force you to override this method for the class.

For example with these two interfaces :

public interface Foo {
default void doThat() {
// ...
}
}

public interface Bar {
default void doThat() {
// ...
}
}

It will not compile :

public class FooBar implements Foo, Bar{
}

You should define/override the method to remove the ambiguity.

You could for example delegate to the Bar implementation such as :

public class FooBar implements Foo, Bar{    
@Override
public void doThat() {
Bar.super.doThat();
}
}

or delegate to the Foo implementation such as : :

public class FooBar implements Foo, Bar {
@Override
public void doThat() {
Foo.super.doThat();
}
}

or still define another behavior :

public class FooBar implements Foo, Bar {
@Override
public void doThat() {
// ...
}
}

That constraint shows that Java doesn't allow multiple inheritancy even for interface default methods.


I think that we cannot apply the same logic for multiple inheritances because multiples issues could occur which the main are :

  • overriding/removing the ambiguity for a method in both inherited classes could introduce side effects and change the overall behavior of the inherited classes if they rely on this method internally. With default interfaces this risk is also around but it should be much less rare since default methods are not designed to introduce complex processings such as multiple internal invocations inside the class or to be stateful (indeed interfaces cannot host instance field).
  • how to inherit multiple fields ? And even if the language allowed it you would have exactly the same issue as this previously quoted : side effect in the behavior of the inherited class : a int foo field defined in a A and B class that you want to subclass doesn't have the same meaning and intention.

Java does not support multiple inheritance, but in case of interface it does! what does it really mean?

Interfaces are like templates and they do not provide any implementation . They are just meant to provide a kind of guideline to the implementing classes. A class may implement any number of intefaces but can extend only one class. The reason for this is that extending two classes that have a same method may create ambiguity when the same method is referred to from the child class (Since it is implemented). But since intefaces do not provide implementation , the implementing class will implement the method , which satisfies the implementation compulsion for both the interfaces . Quoting from the java lanuguage specification

It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:

Example:-

interface Fish { int getNumberOfScales(); }
interface Piano { int getNumberOfScales(); }
class Tuna implements Fish, Piano {

int getNumberOfScales() { return 91; }
}

the method getNumberOfScales in class Tuna has a name, signature, and return type that matches the method declared in interface Fish and also matches the method declared in interface Piano; it is considered to implement both.

Now we can see this is allowed in java, and whenever we do something like this:-

Tuna t = new Tuna();
t.getNumberOfScales();

We know it will always point one function without ambiguity.

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

if an interface were inherited by only one class, it would be useless

I agree, but you can read a lot of discussion on this topic in Java Interfaces Methodology: Should every class implement an interface?

when an interface is inherited by multiple classes, it makes no difference unless it is used for polymorphism

I agree, it would be silly to implement an interface and then bypass it by invoking its methods directly on the concrete class. The purpose of implementing an interface is to have its abstract methods called, which is polymorphism in action. Since polymorphism is the primary tool of Object Oriented Programming, you could take this statement a step further and say that OOP makes no difference unless it is used for polymorphism.

the only thing that makes implementation different from extension is multiple inheritances

No, the primary difference between these Java constructs is that interface implementation facilitates stateless inheritance whereas class extension facilitates stateful inheritance. Java happens to support multiple stateless inheritance and single stateful inheritance. This dichotomy may be considered a mistake.

Do interfaces have any purpose besides achieving polymorphism and multiple inheritance?

In practice, yes. You can find interfaces used for other purposes such as constant interfaces or marker interfaces. Whether these practices are advisable has been debated.

Why is Multiple Inheritance not allowed in Java or C#?

The short answer is: because the language designers decided not to.

Basically, it seemed that both the .NET and Java designers did not allow multiple inheritance because they reasoned that adding MI added too much complexity to the languages while providing too little benefit.

For a more fun and in-depth read, there are some articles available on the web with interviews of some of the language designers. For example, for .NET, Chris Brumme (who worked at MS on the CLR) has explained the reasons why they decided not to:

  1. Different languages actually have different expectations for how MI
    works. For example, how conflicts are
    resolved and whether duplicate bases
    are merged or redundant. Before we can
    implement MI in the CLR, we have to do
    a survey of all the languages, figure
    out the common concepts, and decide
    how to express them in a
    language-neutral manner. We would also
    have to decide whether MI belongs in
    the CLS and what this would mean for
    languages that don't want this concept
    (presumably VB.NET, for example). Of
    course, that's the business we are in
    as a common language runtime, but we
    haven't got around to doing it for MI
    yet.

  2. The number of places where MI is truly appropriate is actually quite
    small. In many cases, multiple
    interface inheritance can get the job
    done instead. In other cases, you may
    be able to use encapsulation and
    delegation. If we were to add a
    slightly different construct, like
    mixins, would that actually be more
    powerful?

  3. Multiple implementation inheritance injects a lot of complexity into the
    implementation. This complexity
    impacts casting, layout, dispatch,
    field access, serialization, identity
    comparisons, verifiability,
    reflection, generics, and probably
    lots of other places.

You can read the full article here.

For Java, you can read this article:

The reasons for omitting multiple
inheritance from the Java language
mostly stem from the "simple, object
oriented, and familiar" goal. As a
simple language, Java's creators
wanted a language that most developers
could grasp without extensive
training. To that end, they worked to
make the language as similar to C++ as
possible (familiar) without carrying
over C++'s unnecessary complexity
(simple).

In the designers' opinion, multiple
inheritance causes more problems and
confusion than it solves. So they cut
multiple inheritance from the language
(just as they cut operator
overloading). The designers' extensive
C++ experience taught them that
multiple inheritance just wasn't worth
the headache.

why Java class can extends only one class but implements many interfaces?

Being able to extend only one base class is one way of solving the diamond problem. This is a problem which occurs when a class extends two base classes which both implement the same method - how do you know which one to call?

A.java:

public class A {
public int getValue() { return 0; }
}

B.java:

public class B {
public int getValue() { return 1; }
}

C.java:

public class C extends A, B {
public int doStuff() {
return super.getValue(); // Which superclass method is called?
}
}

Since interfaces cannot have implementations, this same problem does not arise. If two interfaces contain methods that have identical signatures, then there is effectively only one method and there still is no conflict.

why can a class implement multiple interfaces?

Conceptual Example

The way I think about the multiple interfaces is interface is like the verb or adjective, and class is like the subject.

A tiger can run, so the Tiger class may implement Runnable Interface.

A tiger can eat, so the Tiger class may implement Eatable Interface.

Because an instance of the class could have different behaviors, we could have different corresponding interfaces.

Realistic Example

java.util Class HashMap<K,V>

It implements Serializable, Cloneable, Map<K,V>

All of the interfaces are the characteristics of Class HashMap.



Related Topics



Leave a reply



Submit