Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Interface Inheritance

Tags:

c#

interface

I'm implementing a set of classes and corresponding interfaces where I want each class to have a set of common properties and a set of specialised properties that are specific only to that class. So, I'm considering defining interfaces along the lines of:

interface ICommon {...}  // Members common to all widgets
interface IWidget1 {...} // specialized members of widget type 1
interface IWidget2 {...} // specialized members of widget type 2

I am trying to choose between having the inheritance in the interfaces, or in the class. So, specifically, I can either do it like this:

interface IWidget1 : ICommon {...}
interface IWidget2 : ICommon {...}
class Widget1 : IWidget1 {...}
class Widget2 : IWidget2 {...}

...or like this...

class Widget1: ICommon, IWidget1 {...}
class Widget2: ICommon, IWidget2 {...}

Is there any compelling reason to go one way or the other?

Update: Would it affect the answer if the classes must be COM-visible?

like image 262
Tim Long Avatar asked Mar 03 '10 06:03

Tim Long


People also ask

Is multiple inheritance allowed in interface?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

Does Java support multiple inheritance through interface?

The Java programming language supports multiple inheritance of type, which is the ability of a class to implement more than one interface.

How many interfaces can be inherited?

Interfaces can inherit from one or more interfaces. The derived interface inherits the members from its base interfaces. A class that implements a derived interface must implement all members in the derived interface, including all members of the derived interface's base interfaces.

What is the difference between interface and multiple inheritance?

The difference between inheritance and interface is that inheritance is to derive new classes from existing classes and interfaces is to implement abstract classes and multiple inheritance.


1 Answers

You should choose interface inheritance if and only if a type implementing IWidget1 must also implement ICommon. In either case, the class is going to implement both IWidget1 and ICommon separately. The only difference is that if you make IWidget1 "derive" from ICommon you enforce the fact that an IWidget1 must also be an ICommon.

A good example is IEnumerable and ICollection. Every ICollection is guaranteed to be IEnumerable, so ICollection derives from IEnumerable. If it was legal or made sense to be a collection but not be enumerable, then implementers of ICollection wouldn't have to implement IEnumerable also.

Whichever you choose will not affect COM visibility. .NET will still export the interfaces separately if I recall correctly.

like image 200
Josh Avatar answered Sep 28 '22 05:09

Josh