views:

515

answers:

6

What are the pros and cons of having multiple inheritance?

And why don't we have multiple inheritance in C#?

UPDATE Ok so it is currently avoided because of the issue with clashes resolving which parent method is being called etc. Surely this is a problem for the programmer to resolve. Or maybe this could be resolve simularly as SQL where there is a conflict more information is required i.e. ID might need to become Sales.ID to resolve a conflict in the query.

+2  A: 

Here is a good discussion on the pitfalls of multiple inheritance:

http://stackoverflow.com/questions/406081/why-should-i-avoid-multiple-inheritance-in-c

Here is a discussion from the C# team on why they decided not to allow multiple inheritance:

Andy White
+1  A: 

The main con is that if two classes have a method with the same name, the new subclass doesn't know which one to call.

In C# you can do a form of multiple inheritance by including instances of each parent object within the child.

class MyClass
{
    private class1 : Class1;
    private class2: Class2;

    public MyClass
    {
        class1 = new Class1;
        class2 = new Class2;
    }
    // Then, expose whatever functionality you need to from there.
}
Tim Sullivan
+1  A: 

I will give a pro here based on a C++ report-writer I've been converting to REALbasic (which has interfaces but only single-inheritance).

Multiple inheritance makes it easier to compose classes from small mixin base classes that implement functionality and have properties to remember state. When done right, you can get a lot of reuse of small code without having to copy-and-paste similar code to implement interfaces.

Fortunately, REALbasic has extends methods which are like the extension methods recently added to C# in C# 3.0. These help a bit with the problem, especially as they can be applied to arrays. I still ended up with some class hierarchies being deeper as a result of folding in what were previously multiply-inherited classes.

Andy Dent
A: 

When you inherit from something you are asserting that your class is of that (base) type in every way except that you may implement something slightly differently or add something to it, its actually extremely rare that your class is 2 things at once. Usually it just has behavour common to 2 or more things, and a better way to describe that generally is to have your class implement multiple interfaces. (or possibly encapsulation, depending on your circumstances)

Tim Jarvis
A: 

It's one of those help-me-to-not-shoot-myself-in-the-foot quirks, much like in Java.

Although it is nice to extend fields and methods from multiple sources (imagine a Modern Mobile Phone, which inherits from MP3 Players, Cameras, Sat-Navs, and the humble Old School Mobile Phone), clashes cannot be resolved by the compiler alone.

Beau Martínez
+3  A: 

It's just another tool in the toolbox. Sometimes, it is exactly the right tool. If it is, having to find a workaround because the language actually prohibits it is a pain and leads to good opportunities to screw it up.

Pros and cons can only be found for a concrete case. I guess that it's quite rare to actually fit a problem, but who are the language designers to decide how I am to tackle a specific problem?

Svante
Well in C# it isn't a Tool in the box yet! :)
Harry
+1, but from my understanding, it's only the only right choice in rare few cases.
hasen j
Just as a "typical textbook example": A house cat is both a mammal and a pet. Expressing this as composition is awkward at best. There are mammals that aren't pets, and pets that aren't mammals. As a pet, a cat could have an owner, a loyalty stat, a tax number. As a mammal, it could have feeding status, length overall, tail length, fur colour.
Svante