Monday, April 16, 2012

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


I know that multiple inheritance is not allowed in Java and C#. Many books just say, multiple inheritance is not allowed. But it can be implemented by using interfaces. Nothing is discussed about why it is not allowed. Can anybody tell me precisely why it is not allowed?



Source: Tips4all

9 comments:

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



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

    ReplyDelete
  2. Multiple inheritance of implementation is what is not allowed.

    The problem is that the compiler/runtime cannot figure out what to do if you have a Cowboy and an Artist class, both with implementations for the draw() method, and then you try to create a new CowboyArtist type. What happens when you call the draw() method? Is someone lying dead in the street, or do you have a lovely watercolor?

    I believe it's called the double diamond inheritance problem.

    ReplyDelete
  3. The main (although by no means the only) reason people steer away from MI is the so called "diamond problem" leading to ambiguity in your imlpementation. This wikipedia article discusses it and explains better than I could. MI can also lead to more complex code, and a lot of OO designers claim that you do't need MI, and if you do use it your model is probably wrong. I'm not sure I agree with this last point, but keeping things simple is always a good plan.

    ReplyDelete
  4. In C++ multiple inheritance was a major headache when used improperly. To avoid those popular design issues multiple interfaces "inheritance" was forced instead in modern languages (java, C#).

    ReplyDelete
  5. Another reason is that single-inheritance makes casting trivial, emitting no assembler instructions (other than checking for the compatibility of the types where required). If you had multiple-inheritance, you'd need to figure out where in the child class a certain parent starts. So performance is certainly a perk (although not the only one).

    ReplyDelete
  6. Back in the old days ('70s) when Computer Science was more Science and less mass production the programmers had time to think about good design and good implementation and as a result the products (programms) had high quality ( eg. TCP/IP design and implementation ).
    Nowadays, when everybody is programming, and the managers are changing the specs before deadlines, subtle issues like the one descriped in the wikipedia link from Steve Haigh post are difficult to track; therefore, the "multiple inheritance" is limited by compiler design. If you like it, you can still use C++ .... and have all the freedom you want :)

    ReplyDelete
  7. Because Java has a greatly different design philosophy from C++. (I'm not going to discuss C# here.)

    In designing C++, Stroustrup wanted to include useful features, regardless of how they could be misused. It's possible to screw up big-time with multiple inheritance, operator overloading, templates, and various other features, but it's also possible to do some very good things with them.

    The Java design philosophy is to emphasize safety in language constructs. The result is that there are things that are a lot more awkward to do, but you can be a lot more confident that the code you're looking at means what you think it does.

    Further, Java was to a large extent a reaction from C++ and Smalltalk, the best known OO languages. There are plenty of other OO languages (Common Lisp was actually the first one to be standardized), with different OO systems that handle MI better.

    Not to mention that it's entirely possible to do MI in Java, using interfaces, composition, and delegation. It's more explicit than in C++, and therefore is clumsier to use but will get you something you're more likely to understand at first glance.

    There is no right answer here. There are different answers, and which one is better for a given situation depends on applications and individual preference.

    ReplyDelete
  8. Multiple Inheritance is


    hard to understand
    hard to debug (for example, if you mix classes from multiple frameworks that have identically-named methods deep down, quite unexpected synergies can occur)
    easy to mis-use
    not really that useful
    hard to implement, especially if you want it done correctly and efficiently


    Therefore, it can be considered a wise choice to not include Multiple Inheritance into the Java language.

    ReplyDelete
  9. I take the statement that "Multiple inheritance is not allowed in Java" with a pinch of salt.

    Multiple Inheritance is defined when a "Type" inherits from more than one "Types". And interfaces are also classified as types as they have behavior. So Java does have multiple inheritance. Just that it is safer.

    ReplyDelete