Inheritance of multiple interfaces

I implement a set of classes and corresponding interfaces, where I want each class to have a set of common properties and a set of specialized properties specific to this class only. So, I am considering defining interfaces line by line:

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 inheritance in interfaces or in class. Therefore, in particular, I can either do it like this:

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

... or like that ...

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

Is there any good reason to go anyway?

Update: will this affect the answer if classes should be visible by COM?

+5
source share
4 answers

, , IWidget1, ICommon. IWidget1, ICommon . , IWidget1 "" ICommon, , IWidget1 ICommon.

IEnumerable ICollection. ICollection IEnumerable, ICollection IEnumerable. , , ICollection IEnumerable.

, COM..NET - , .

+6

, .

IWidget1 , ICommon1, IWidget1 ICommon1. , .

+3

, , , .

IWidgetX ICommon, IWidget1 IWidget2, :

class Widget3 : IWidget1, IWidget2

ICommon, ICommon . , .

, IWidgetX ICommon, :

class Widget3 : IWidget1, IWidget2, ICommon

, , Widget3 - IWidgetX ICommon

+1

Inheritance depends on the attribute / behavior of the class or interface. If the behavior in IWidget1and IWidget2includes all the behaviors in ICommon, then you can inherit, for example, IWidget1 : ICommonand IWidget2 : ICommon, and there is no problem with ComVisible. This is just an OOPS concept.

0
source

All Articles