views:

2020

answers:

9

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?

+3  A: 

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.

Steve Haigh
+20  A: 

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.

duffymo
double diamond is the beer! ;)
Mitch Wheat
You get a lovely watercolor of someone dead on the street :-)
Dan F
Is this the only problem? I think, I'm not sure, that the C++ solves this problem by the virtual keyword, is this true? I'm not good at C++
CodingTales
LOL @ "Is someone lying dead in the street, or do you have a lovely watercolor?" +1 :D
annakata
In C++ you can either specify which of the base class functions to call in derived or re-implement it yourself.
Dmitry Risenberg
Modern design tends to favour composition over inheritance in all but the simplest cases. Multiple inheritance would never have counted as a simple case. With composition, you have precise control over what your class does when there are diamond problems such as this...
Bill Michell
You'd simply need a priority list. This is used in Common Lisp's object system, CLOS. All the classes form a heirachy and the method that is called is determined from there. Further, CLOS allows you to define different rules such that a CowboyArtist.draw() might first draw a Cowboy and then an Artist. Or what ever you make up.
Sebastian Krog
@iSattar: No, virtual keyword does not sort it out. @Bill Mitchell, agreed with your comment about composition. This "simple" case was going for a punchline and wasn't intended to be realistic. @Sebastian, thanks for the CLOS lesson. I don't know Lisp, but I'd like to.
duffymo
+33  A: 

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.

Razzie
Nice comparison. Quite indicative of the thought-processes that underlie both platforms I think.
CurtainDog
+2  A: 

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#).

inazaruk
+2  A: 

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

Blindy
+2  A: 

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 :)

... including the freedom to shoot yourself in the foot, multiple times ;)
Mario Ortegón
A: 

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.

Rig Veda
But you don't inherit from an interface, you implement it. Interfaces aren't classes.
Blorgbeard
Yes, but inheritance was never so narrowly defined, except in some prelim books. And I think we should look beyond the grammar of it.They both do the same thing,and interfaces were "invented" only for that reason.
Rig Veda
Upvoting to remove the -1. You've got a good point.
David Thornley
A: 

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.

mfx
I disagree with all the above, having worked with the Common Lisp Object System.
David Thornley
Dynamic languages don't count ;-) In any Lisp-like system, you have a REPL that makes debugging rather easy. Also, CLOS (as i understand it, i have never really used it, only read about it) is a meta-object-system, with a lot of flexibility and a roll-your-own attitude. But consider a statically compiled language like C++, where the compiler generates some fiendishly complex method lookup using multiple (possibly overlapping) vtables: in such an implementation, finding out what implementation of a method was invoked might be a not-so-trivial task.
mfx
+2  A: 

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.

David Thornley