views:

3332

answers:

9

I can see people asking all the time whether multiple inheritance should be included into the next version of C# or Java. C++ folks, who are fortunate enough to have this ability, say that this is like giving someone a rope to eventually hang themselves.

What’s the matter with multiple inheritance? Are there any concrete samples?

+16  A: 

let's say you have objects A and B which are both inherited by C. A and B both implement foo() and C does not. I call C.foo(). Which implementation gets chosen? There are other issues, but this type of thing is a big one.

tloach
But that's not really a concrete example. If both A and B have a function, it's very likely that C will need it's own implementation as well. Otherwise it can still call A::foo() in it's own foo() function.
QuantumPete
@Quantum: What if it doesn't though? It's easy to see the problem with one level of inheritance, but if you have lots of levels and you have some random function that is somewhere twice this becomes a very hard problem.
tloach
Also, the point isn't that you can't call the method A or B by specifying which one you want, the point is that if you don't specify then there's no good way to choose one. I'm not certain how C++ handles this, but if someone knows could the mention it?
tloach
@tloach - if C does not resolve the ambiguity, the compiler can detect this error and return a compile-time error.
Eamon Nerbonne
@Earmon - Due to polymorphism, if foo() is virtual the compiler might not even know at compile-time that this is going to be an issue.
tloach
+3  A: 

The main problem with multiple inheritance is nicely summed up with tloach's example. When inheriting from multiple base classes that implement the same function or field it's the compiler has to make a decision about what implementation to inherit.

This get's worse when you inherit from multiple classes that inherit from the same base class. (diamond inheritance, if you draw the inheritance tree you get a diamond shape)

These problems are not really problematic for a compiler to overcome. But the choice the compiler has to make here are rather arbitrary, this make code far less intuitive.

I find that when doing good OO design I never need multiple inheritance. In cases I do need it I usually find I've been using inheritance to reuse functionality while inheritance is only appropriate for "is-a" relations.

There are other techniques like mixins that solve the same problems and don't have the issues that multiple inheritance has.

Mendelt
The compiled *doesn't* need to make an arbitrary choice - it can simply error out. In C#, what's the type of `([..bool..]? "test": 1)`?
Eamon Nerbonne
+10  A: 

The diamond problem - http://en.wikipedia.org/wiki/Diamond_problem

J Francis
+7  A: 

Multiple inheritance is one of those things that is not used often, and can be misused, but is sometimes needed.

I never understood not adding a feature, just because it might be misused, when there are no good alternatives. Interfaces are not an alternative to multiple inheritance. For one, they don't let you enforce preconditions or postconditions. Just like any other tool, you need to know when it is appropriate to use, and how to use it.

KeithB
+17  A: 

The most obvious problem is with function overriding.

Let's say have two classes A and B, both of which define a method "doSomething". Now you define a third class C, which inherits from both A and B, but you don't override the "doSomething" method.

When the compiler seed this code...

C c = new C();
c.doSomething();

...which implementation of the method should it use? Without any further clarification, it's impossible for the compiler to resolve the ambiguity.

Besides overriding, the other big problem with multiple inheritance is the layout of the physical objects in memory.

Languages like C++ and Java and C# create a fixed address-based layout for each type of object. Something like this:

class A:
    at offset 0 ... "abc" ... 4 byte int field
    at offset 4 ... "xyz" ... 8 byte double field
    at offset 12 ... "speak" ... 4 byte function pointer

class B:
    at offset 0 ... "foo" ... 2 byte short field
    at offset 2 ... 2 bytes of alignment padding
    at offset 4 ... "bar" ... 4 byte array pointer
    at offset 8 ... "baz" ... 4 byte function pointer

When the compiler generates machine code (or bytecode), it uses those numeric offsets to access each method or field.

Multiple inheritance makes it very tricky.

If class C inherits from both A and B, the compiler has to decide whether to layout the data in AB order or in BA order.

But now imagine that you're calling methods on a B object. Is it really just a B? Or is it actually a C object being called polymorphically, through its B interface? Depending on the actual identity of the object, the physical layout will be different, and its impossible to know the offset of the function to invoke at the call-site.

The way to handle this kind of system is to ditch the fixed-layout approach, allowing each object to be queried for its layout before attempting to invoke the functions or access its fields.

So...long story short...it's a pain in the neck for compiler authors to support multiple inheritance. So when someone like Guido van Rossum designs python, or when Anders Hejlsberg designs c#, they know that supporting multiple inheritance is going to make the compiler implementations significantly more complex, and presumably they don't think the benefit is worth the cost.

benjismith
If you updated your answer with a description in layman terms of the obvious problem with overriding it would be my pleasure to select this answer as accepted.
Totophil
No problem. Consider it done.
benjismith
Ehm, Python supports MI
Nemanja Trifunovic
These aren't very convincing arguments - the fixed layout thing isn't tricky at all in most languages; in C++ it's tricky because memory isn't opaque and thus you may run into some difficulty with pointer arithmetic assumptions. In languages where class *definitions* are static (as in java, C# and C++), multiple inheritance name clashes can be forbidden compile time (and C# does this anyhow with interfaces!).
Eamon Nerbonne
The OP just wanted to understand the issues, and I explained them without personally editorializing on the matter. I just said that the language designers and compiler implementors "presumably don't think the benefit is worth the cost".
benjismith
+7  A: 

The problems you guys mention are not really that hard to solve. In fact e.g. Eiffel does that perfectly well! (and without introducing arbitrary choices or whatever)

E.g. if you inherit from A and B, both having method foo(), then of course you don't want an arbitrary choice in your class C inheriting from both A and B. You have to either redefine foo so it's clear what will be used if c.foo() is called or otherwise you have to rename one of the methods in C. (it could become bar())

Also I think that multiple inheritance is often quite useful. If you look at libraries of Eiffel you'll see that it's used all over the place and personally I've missed the feature when I had to go back to programming in Java.

I agree. The main reason why people hate MI is the same as with JavaScript or with static typing: most people have only ever used very bad implementations of it – or have used it very badly. Judging MI by C++ is like judging OOP by PHP or judging automobiles by Pintos.
Jörg W Mittag
Out of curiosity, what would you say are good languages by which to judge MI?
Devon_C_Miller
Eiffel gets it right. Or at least less wrong.
Jörg W Mittag
The Common Lisp Object System handles MI well (although it's rather idiosyncratic in its approach to OO). If you're into text adventures, now often known as interactive fiction, you'll find languages that rely heavily on MI at ifarchive.org.
David Thornley
+1  A: 

There is nothing wrong in multiple inheritance itself. The problem is to add multiple inheritance to a language that was not designed with multiple inheritance in mind from the start.

The Eiffel language is supporting multiple inheritance without restrictions in a very efficient and productive way but the language was designed from that start to support it.

This feature is complex to implement for compiler developers, but it seems that that drawback could be compensated by the fact that a good multiple inheritance support could avoid the support of other features (i.e. no need for Interface or Extension Method).

I think that supporting multiple inheritance or not is more a matter of choice, a matter of priorities. A more complex feature takes more time to be correctly implemented and operational and may be more controversial. The C++ implementation may be the reason why multiple inheritance was not implemented in C# and Java...

Chris
+1  A: 

The Common Lisp Object System (CLOS) is another example of something that supports MI while avoiding the C++-style problems: inheritance is given a sensible default, while still allowing you the freedom to explicitly decide how exactly to, say, call a super's behaviour.

Frank Shearar
+1  A: 

I don't think the diamond problem is a problem, I would consider that sophistry, nothing else.

The worst problem, from my point of view, with multiple inheritance is RAD - victims and people who claim to be developers but in reality are stuck with half - knowledge (at best).

Personally, I would be very happy if I could finally do something in Windows Forms like this (it's not correct code, but it should give you the idea):

public sealed class CustomerEditView : Form, MVCView<Customer>

This is the main issue I have with having no multiple inheritance. You CAN do something similar with interfaces, but there is what I call "s*** code", it's this painful repetitive c*** you have to write in each of your classes to get a data context, for example.

In my opinion, there should be absolutely no need, not the slightest, for ANY repetition of code in a modern language.

Turing Complete