Why doesn’t the Java language support multiple inheritance?

There are 2 reasons mentioned that will give you a idea why we don’t have multiple inheritance in java.

1st is ambiguity around Diamond problem, consider a class A has foo() method and then B and C derived from A and has there own foo() implementation and now class D derive from B and C using multiple inheritance and if we refer just foo() compiler will not be able to decide which foo() it should invoke. This is also called Diamond problem because structure on this inheritance scenario is similar to 4 edge diamond, see below

          A foo()
        /      \
      /          \
   B foo()     C foo()
      \          /
        \     /
         D foo()

Even if we remove the top head of diamond class A and allow multiple inheritances we will see this problem of ambiguity.

2nd reason: Multiple inheritances does complicate the design and creates problem during casting, constructor chaining etc and given that there are not many scenario on which you need multiple inheritance its wise decision to omit it for the sake of simplicity. Also java avoids this ambiguity by supporting single inheritance with interfaces. Since interface only have method declaration and doesn’t provide any implementation there will only be just one implementation of specific method hence there would not be any ambiguity.

One Thought on “Why doesn’t the Java language support multiple inheritance?

  1. But Java do support multiple inheritance. How about this code:

    interface A { int x = 1; }

    interface B { int x = 2; }

    class Impl implements A, B
    {{
    // Does not compile:
    // int what = x;

    // Compiles:
    int what = B.x;
    }}

    Also, ambiguity can be resolved by a clearly defined procedure/algorithm as has Java 1.8 done with the new default method implementations.

Leave a Reply to Martin Andersson Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation