none
Multiple inheritance in C#?

    General discussion

  • Friends, Romans and Countrymen!

    I have heard many programmers speak of the implementation of many inferfaces by a C# class as a case of multiple inheritance. Is such a view correct?

    My opinion is that it is not multiple ineritance at all, although it syntactically looks like it. One of the key objectives of inheritance is code reuse. But since there is no code in an interface (I mean, no implementation of methods or properties), the question of code reuse doesn't arise here at all. So implementing multiple interfaces is not a case of multiple inheritance.

    What do my fellow Romans, Friends and Countrymen say on this? Do let me know your thoughts.

    Brutus Maximus

    Friday, September 09, 2011 11:12 AM

All replies

  • I believe that you're correct, Brutus, for exactly the reasons you outline.

    A class that implements an interface is merely forced to implement the methods of that interface, it doesn't inherit a thing.

    Friday, September 09, 2011 11:22 AM
  • Hi,

    That is a good point!

    On one hand, I see the definition on http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)

    Regarding the definition there, the use of interfaces is polymorphism and not inheritance.

    On the other side, we have the C# Language Specification 4.0 where in chapter 1.9 the word inheritance is used for interfaces. So they have something like

    interface A { }
    interface B : A { }
    Interface B inherits Interface A.

    So personaly I agree to you and wikipedia: It is polymorphism and not inheritance. But I know, that others do not agree to this definition so I am understanding, what they are saying, too.

    With kind regards,

    Konrad

    Friday, September 09, 2011 11:36 AM
  • One interface can inherit many other interfaces. You can have a hierarchy of interfaces, which can have multiple base-interfaces. You can cast into any one of the base-interfaces... That is multiple inheritance. Even with interface, you can have code-reuse... of course, just outside of the class. My opinion: Inheritance ist not just the part of "Class A uses the code of class B".

    Of course, C# doesn't support full multiple inheritance like C++, because classes can not have multiple base-classes, and the source code can not be used from multiple base classes - so what...?

    But finally, I think, that this discussion could become an endless discussion, based on opinions... Because the isn't a one and only "true definition of multiple inheritance in programming".

    Friday, September 09, 2011 11:36 AM
  • Multiple inheritence, by .net definition, does not exist. Using a bunch of interfaces doesn't create multiple inheritance. It just creates a bunch of interfaces.

    Adam


    Already reported as abusive
    Friday, September 09, 2011 11:51 AM
  • Multiple inheritence, by .net definition, does not exist. Using a bunch of interfaces doesn't create multiple inheritance. It just creates a bunch of interfaces.

    But exactly that is stated inside the C# language specification 4.0 in chapter 1.9:

    Interfaces may employ multiple inheritance. In the following example, the interface IComboBox inherits from both ITextBox and IListBox.

    interface IControl
    {
       void Paint();
    }

    interface ITextBox: IControl
    {
       void SetText(string text);
    }

    interface IListBox: IControl
    {
       void SetItems(string[] items);
    }

    interface IComboBox: ITextBox, IListBox {}

    Classes and structs can implement multiple interfaces. In the following example, the class EditBox implements both IControl and IDataBound.

    interface IDataBound
    {
       void Bind(Binder b);
    }

    public class EditBox: IControl, IDataBound
    {
       public void Paint() {...}

       public void Bind(Binder b) {...}
    }

    So it is simply a question of the exact definition used but as usual: You do not find clear definitions of the used words. So I just look around and try to understand, what people mean with their words and do not start a discussion. but in OOP terms, a class implementing an interface is called polymorphism and not Inheritance. (I posted a link to wikipedia already where this is explained a little more.)

    With kind regards,

    Konrad

    Friday, September 09, 2011 12:20 PM
  • Interfaces can inherit from multiple other base interfaces.

    Classes can inherit from only one base class.

    Classes cannot inherit from any interface, they only implement them.

    Monday, September 12, 2011 7:54 AM
  • My deepest thanks to all my Friends, Romans and Countrymen who took the time and effort to come around here and post their replies!

    So it seems that since interfaces don't derive from System.Object, they avoid the diamond problem, hence they are free to inherit from multiple base interfaces. Though I wonder what they 'inherit'!

    Brutus

    Thursday, September 15, 2011 11:03 AM
  • They inherit properties and behaviour, but not implementation.

    Thursday, September 15, 2011 11:07 AM
  • They inherit properties and behaviour, but not implementation.

    Now aren't properties and behaviour 'imposed' on classes by the interfaces they implement? Is that inheritance?
    Monday, August 05, 2013 3:17 PM