4

Possible Duplicate:
Does C# support multiple inheritance?

Does C# support multiple inheritance?

Community
  • 1
  • 1
kumarsram
  • 601
  • 1
  • 6
  • 11

8 Answers8

9

C# does not support multiple inheritance. It does support multiple implementation of interfaces (so your class can uses as many interfaces as it likes, only one base class tho')

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
3

In short No, multiple inheritance is not supported.

This is not a problem as inheritance shall always translate to a "is-a" relationship. Objects usually are only of one kind.

On the other side interfaces, that translate to a "behaves like" relationship can be implemented multiple times.

This is not a limitation but rather an advantage to avoid many strange issues like the Diamond Problem.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
2

No. Sorry. It currently does not support multiple inheritance.

Richard
  • 21,728
  • 13
  • 62
  • 101
  • 5
    Because I am polite and I guess he was hoping it did support multiple inheritance. Oh and I am English so I am sorry about everything ;-) – Richard Nov 02 '10 at 12:33
2

It does not support multiple inheritance, and hopefully it never does. Getting rid of this error source was one of the major steps away from the C++ programming model.

Thomas

Thomas Weller
  • 11,631
  • 3
  • 26
  • 34
  • Taken by Java long before C# existed. And it's not that major a deal for people who know how to do it properly. – duffymo Nov 02 '10 at 12:14
  • Exactly nothing is a big deal if it is done by skilled people. The problem is that you have to care about the not so skilled people if you want to create a robust product - really smart people can cope with everything. And also, taken strictly, multiple inheritance is a design smell... – Thomas Weller Nov 02 '10 at 12:30
  • I disagree. Depends on your definition of strict. If I have multiple inheritance of implementation, and there's no overlap of method signatures, what's the harm? You're being excessively dogmatic in my opinion. – duffymo Nov 02 '10 at 15:27
  • Maybe I am - but I have good reasons for it. This gives you an overview of how you can shhot yourself in the foot with MI: http://stackoverflow.com/questions/225929/what-is-the-exact-problem-with-multiple-inheritance/226056#226056. And the problem is: if there's the possibility to do so, people WILL do it. I know this kind of stuff quite well, because as a freelancer in the area of 'Code Quality', I see such things much too often... – Thomas Weller Nov 02 '10 at 15:43
1

Multiple Inheritance is always a creepy concept when you conceptualize with real world examples. Multiple Inheritance means Multiple Fathers. Objects are safe in C# they belong to only one father :)


On a different threat they have discussed some of the design problems it has. This must interest you: What is the exact problem with multiple inheritance?

Community
  • 1
  • 1
Ramiz Uddin
  • 4,249
  • 4
  • 40
  • 72
1

Multiple inheritance is not supported in C#.

But if you want to "inherit" behavior from two sources why not use the combo:

  • Composition

    and

  • Dependency Injection

There is a basic but important OOP principle that say: "Favor composition over inheritance".

You can create a class like this:

public class MySuperClass
{
    private IDependencyClass1 mDependency1;
    private IDependencyClass2 mDependency2;

    public MySuperClass(IDependencyClass1 dep1, IDependencyClass2 dep2)
    {
        mDependency1 = dep1;
        mDependency2 = dep2;
    }

    private void MySuperMethodThatDoesSomethingComplex()
    {
        string s = mDependency1.GetMessage();
        mDependency2.PrintMessage(s);
    }
}

As you can see the dependecies (actual implementations of the interfaces) are injected via the constructor. You class does not know how each class is implemented but it knows how to use them. Hence a loose coupling between the classes involved here but the same power of usage.

Today's trends show that inheritance is kind of "out of fashion".

Community
  • 1
  • 1
Liviu Mandras
  • 6,540
  • 2
  • 41
  • 65
0

My 2 cents: as has already been said, C# doesn't support multiple inheritance as also other OO languages like Java. The main reason is the problems that multiple inheritance brings, like the use of the super() constructors, as example; it becomes difficult to evaluate all the possible ambiguities -- from which class does the super() comes from? Also there is the problem of performance because of the ways to solve the ambiguities. Most of this problems where discovered with C++ that supports multiple inheritance and, with that in mind, the developers

As Svisstack said, use interfaces. Most of the time is the best way to solve your problem. By experience, even in C++ I use abstract classes without any attribute to simulate an interface.

PS: as I was writting this answer, jdehaan present what I was saying more detailed.

jmpcm
  • 1,814
  • 2
  • 19
  • 27
  • Java does not support multiple inheritance of implementation. This is simply wrong. C# followed suit with Java - no multiple inheritance of concrete classes, multiple interfaces are allowed. Get your facts right. – duffymo Nov 02 '10 at 12:16
  • Sorry, I think I haven't explained right. I know that Java don't support multiple inheritance and that was what I was trying to say. So rephrasing myself: "as has already been said, C# doesn't support multiple inheritance, following the path of other OO languages like Java." Sorry for the bad English... – jmpcm Nov 02 '10 at 12:27
0

While it doesn't support multiple inheritance you can push multiple implementations via interfaces and abstracts. An example would be creating a vehicle and implementing different interfaces based on the type of vehicle. Below is a very pseudo example of a plane and a taxi which both implement the vehicle class but then also implement the interface specific to the type of vehicle and contains method specific to their actions.

The way I typed the method calls is known as explicit interface implementation and handles an event where say both the vehicle and other interfaces contain the same method.

interface IPlane
{
void CheckLanding();
void LandingGear();
void Turn();
}
interface IVehicle
{
void Gas();
void Brake();
void Turn();
}
interface ITaxi
{
void StartMeter();
void StopMeter();
)
public class Taxi: IVehicle, ITaxi
{
void ITaxi.StartMeter()
void IVehicle.Gas
void IVehicle.Brake
void ITaxi.StopMeter()
{
public class AirPlane: IVehicle, IPlane
{
void IVehicle.Gas()
void IVehicle.Turn()
void IPlane.LandingGear()
void IPlane.Turn()
void IPlane.CheckLanding()
void IPlane.Turn()
void IPlane.LandingGear()
void IVehicle.Brake()
void IVehicle.Turn()
}
ry_donahue
  • 505
  • 2
  • 13