Home   Cover Cover Cover Cover
 

Multiple interface inheritance

Question: Explain the possible reason why a class in C# can only inherit from a single class, while being able to implement multiple interfaces.

Answer: If a class inherited from several classes this could lead to name conflicts. If two or more base classes contained a method with the name M there would be two inherited versions of M in the subclass. If the M method were called on a subclass object, which of the two inherited methods should be invoked?

With interfaces this ambiguity cannot occur. If a class implements two interfaces each containing an M method, only their signatures are inherited but not their code (because there is no code in the interfaces). The M method must be implemented in the subclass, so no name conflict arises.