January 13, 2010

Multiple Inheritance in C#

Does C# support multiple inheritance?” is a question we have all come across at some time or the other. And the standard answer to it is "No", or “Yes, using interfaces”. That’s where it usually stops.

However, let me challenge you again with “How?”

Typically inheritance would mean the following:

· The derived class Inherits the members of the base class along with the implementation

· It can override one or more of them

· The base class can be substituted for the derived class

If these conditions are fulfilled, we can safely say there is simulated inheritance. In case of multiple inheritances the same some should hold true of the derived class for each of the base classes.

Can interfaces help us achieve these? Let us find out.

I guess an example would be in order.

Let us take three classes which are defined as follows:

At this point, since we already Alarm and Watch classes defined, wouldn’t it be a good idea to be able to inherit them into the CellPhone class?

Since C# doesn’t allow us the luxury of declaring
  CellPhone: Alarm, Watch
...we have to find an alternative, that is interfaces.

By the way, did you know that it is possible to re-factor a class in Visual Studio to automatically extract an interface?

Now our classes look like this.

We should now implement the interfaces in CellPhone which now becomes…

In the CellPhone class we used the Alarm and Watch classes to implement the interfaces for us.

Let us see whether we have achieved (read simulated) inheritance for both Alarm and Watch classes.

- The derived class CellPhone Inherits the members of both the base classes Alarm and Watch along with the implementation

- It is possible to override any of members of IAlarm and IWatch in the CellPhone class

- The base classes (interfaces in this case) IAlarm and IWatch can be substituted for the derived class

There! We have achieved (read simulated) multiple inheritance!

P.S. A lot of comments indicated that this is not inheritance. Yes, I agree. That is the very reason this blog was created to help developers simulate it artificially using interfaces.

7 comments:

  1. This common technique is called delegation, and is preferred in many cases over inheritance.

    ReplyDelete
  2. This isn`t multiple inheritance, this is simple interface implementing ;)

    ReplyDelete
  3. Technically, one *inherits* a base-class and *implements* an interface, but such nitpickiness aside... :-)

    Another approach to consider for this sort of situation, taking a rather different angle than inheritance (which has its own issues) is composition/delegation techniques (as Anonymous said) such as the Strategy pattern. "Has-a" rather than "is-a", so to speak.

    Some good stuff on this approach at (among others) http://tiedyedfreaks.org/eric/CompositionVsInheritance.html
    http://www.netobjectives.com/PatternRepository/index.php?title=TheStrategyPattern

    ReplyDelete
  4. This is not inheritance. An interface requires you provide an implementation each time. So if you were to create

    class IPad : IWatch, IAlarm

    You would implement methods for IWatch and IAlarm again - boo.

    A better idea would be to create a ICellPhone interface that defines properties IWatch and IAlarm. This way you could use the same class to implement alarms and watch functions without having to rewrite them.

    I repeat, this is not multiple inheritance. Interviews that accept that answer are just flat wrong.

    ReplyDelete
  5. What you have done here is not inheritance. It's a composite pattern, implemented with delegation as Anonymous stated.

    ReplyDelete
  6. Thanks.
    People usually stop at answering verbally yes or no to this question. You have done a great job in laying it out as an example.

    Thanks again. Definately helpful.

    Warm Regards,

    Umesh Bhavsar

    ReplyDelete
  7. I am not against the explanation above, But,
    This is not inheritance. You can not just go on generating interface stubs for classes in your project. Moreover, the programmer now has to write their own implementation of all member functions and events, so what is actually being inherited???

    In fact, interfaces are never Inherited, they are implemented. The purpose of interfaces is to ensure and enforce consistency in the application architecture.

    ReplyDelete