Inheritance concept in C #

Possible duplicate:
Does C # support multiple inheritance?

Does C # support multiple inheritance?

+4
source share
8 answers

C # does not support multiple inheritance. It supports multiple interface implementations (so your class can use as many interfaces as it likes, only one tho 'base class)

+9
source

In short No, multiple inheritance is not supported.

This is not a problem, since inheritance always translates into an is-a relationship. Objects usually have only one view.

On the other hand, interfaces that switch to a “behaves like” relationship can be implemented several times.

This is not a limitation, but an advantage to avoid many strange problems, such as the Diamond Problem .

+3
source

No. I'm sorry. It currently does not support multiple inheritance.

+2
source

It does not support multiple inheritance, and I hope he never does. Getting rid of this source of errors was one of the main steps from the C ++ programming model.

Thomas

+2
source

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


Due to another threat, they discussed some design issues. This should interest you: What is the specific problem of multiple inheritance?

+1
source

Multiple inheritance is not supported in C #.

But if you want to “inherit” behavior from two sources, why not use combos:

  • Composition

    and

  • Dependency Injection

There is a basic, but important, principle of OOP that states: "Make composition over inheritance."

You can create a class as follows:

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, dependencies (actual interface implementations) are introduced through the constructor. You class does not know how each class is implemented, but it knows how to use them. Therefore, there is a loose connection between the classes involved here, but with the same degree of use.

Today, trends show that inheritance is a kind of “out of fashion”.

+1
source

My 2 cents: as already mentioned, C # does not support multiple inheritance, as well as other OO languages ​​such as Java. The main reason is problems arising from multiple inheritance, for example, the use of super () constructors; it becomes difficult to evaluate all possible ambiguities - what class does super () come from? There is also a performance issue due to ways to resolve ambiguities. Most of these problems are discovered with C ++, which supports multiple inheritance and, keeping in mind, developers

As Swissstack said, use interfaces. In most cases, this is the best way to solve your problem. From experience, even in C ++ I use abstract classes without any attribute to simulate an interface.

PS: when I wrote this answer, jdehaan presented what I said in more detail.

0
source

While it does not support multiple inheritance, you can use several implementations through interfaces and theses. An example would be the creation of a vehicle and the implementation of various interfaces based on the type of vehicle. Below is a very pseudo example of an airplane and a taxi, which simultaneously implement a vehicle class, but then also implement an interface specific to the type of vehicle and contain a method specific to their actions.

The way I dialed method calls is known as the implementation of an explicit interface and handles an event in which it is said that 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() } 
0
source

All Articles