开发者

Can multiple inheritance be achieved in C# using interfaces? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

I often encounter in articl开发者_如何学编程es which describe abstract class and interface, that C# does not support multiple inheritance but can be achieved using interfaces. I don't agree to that for the following reasons

  1. We always inherit the state and behavior from any class.
  2. Interface does not define the state or behavior.
  3. We cannot inherit anything from an interface but always implement it.

So 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).


No, you can not implement multiple inheritance in C#, period.

You can, however, get some of the benefits of multiple inheritance through interfaces, namely the part where you can add contracts to a class to make it fit other parts of the system, the substitution principle.

But no, you can not get the part where you inherit behavior from multiple base classes.


in fact we can never inherit from an interface

Disputable. "Inheriting" from an interface is a way of viewing it. It does satisfy the substitution principle.

But I agree that "Implementing" an interface is a more common expression/view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜