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

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.


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.


Reason:Java is very popular and easy to code, because of its simplicity.

So what ever java developers feel difficult and complicated to understand for programmers, they tried to avoid it. One such kind of property is multiple inheritance.

  1. They avoided pointers
  2. They avoided multiple inheritance.

Problem with multiple inheritance: Diamond problem.

Example:

  1. Assume that class A is having a method fun(). class B and class C derives from class A.
  2. And both the classes B and C, overrides method fun().
  3. Now assume that class D inherits both class B, and C. (just Assumption)
  4. Create object for class D.
  5. D d = new D();
  6. and try to access d.fun(); => will it call class B's fun() or class C's fun()?

This is the ambiguity existing in diamond problem.

It is not impossible to solve this problem, but it creates more confusion and complexities to the programmer while reading it.It causes more problem than it tries to solve.

Note: But any way you can always implement multiple inheritance indirectly by using interfaces.