0

Can Interfaces be a substitute for Multiple Inheritance in C#?

EDIT: This is a duplicate of: https://stackoverflow.com/questions/579090/why-c-doesnt-support-multiple-inheritence-can-interfaces-be-used-as-a-substi

Which was a duplicate of: Should C# have multiple inheritance?

Community
  • 1
  • 1
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135

3 Answers3

2

Multiple inheritance implies that you inherit functionality from multiple base classes. Since interfaces define interfaces, not implementations, the answer is no.

Szymon Rozga
  • 17,971
  • 7
  • 53
  • 66
1

To some extent, yes. The classic "is-a" relationship that inheritance provides can be expanded with the use of interfaces. Meaning, if a class inherits from another class, and implements 3 interfaces, an object of this class can be passed into a method that takes the base class, or any of the three interfaces as parameters. So, in a sense, this class "is-a" one of 4 different things. Makes sense?

BFree
  • 102,548
  • 21
  • 159
  • 201
0

Conceptually, interfaces can allow you to have multiple is-a relationships with other categories of classes. This is part of why multiple inheritance is used.

For example,

public class TradeReport : IReport, ITrade, IComplianceRecord

Provided the class above implements the methods of all three interfaces meaningfully, you can use it to describe a report, a trade, and a compliance record.

But, that's the limit. There's no implementation inheritance in interfaces.

Yes - that Jake.
  • 16,725
  • 14
  • 70
  • 96