Can multiple inheritance be achieved in C # using interfaces?

I often see articles describing an abstract class and interface that C # does not support multiple inheritance, but can be achieved using interfaces. I disagree with this for the following reasons.

  • We always inherit the state and behavior of any class.
  • The interface does not define state or behavior.
  • We cannot inherit anything from the interface, but we always implement it.

So, the bottom line is that C # does not support multiple inheritance, and interfaces cannot help us achieve multiple inheritance (in fact, we can never inherit from an interface).

+3
source share
2 answers

No, you cannot implement multiple inheritance in C #, period.

However, you can get some advantages of multiple inheritance through interfaces, namely the part where you can add contracts to the class so that they correspond to other parts of the system, the principle of substitution.

But no, you cannot get the part where you inherit the behavior from several base classes.

+3
source

in fact we can never inherit from an interface

Controversial. Inheritance from an interface is a way to view it. It satisfies the principle of substitution.

But I agree that the "implementation" of an interface is a more common expression / representation.

+1
source

Source: https://habr.com/ru/post/1770870/


All Articles