Multiple Inheritance in C#

Learn how to implement multiple inheritance in C#. Inheritance is one of the key characteristics of an object oriented programming language.

Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, Will a Class fully represent an object? Meaning that, if we have a base class with abstract methods designed for a particular application and we know that the inheriting object will only need the methods defined in that class. We now have a valid design pattern here.

The Vehicle Car Object

Lets say we have an abstract class called "Vehicle" as well as another class called "ConstructionVehicle". The vehicle class has methods such as Accelerate() , Stop(), and the "ConstructionVehicle" class has methods such as ExecuteDump() and TurnOnBackUpSound(). If we were only going to build a Car object and know we would only use those methods from the "Automobile" class this would be fine.

The DumpTruck Object

Now we want to create another object called "DumpTruck". We could inherit from the Automobile class but that class does not have the methods that we need called ExecuteDump() and TurnOnBackUpSound(). If we were using a language such as C++ we could easily inherit from both classes using multiple inheritance. However, seeing C# is our language of choice, multiple inheritance is not an option, you may only inherit from one Base Class.

From Abstract Classes to Interfaces

From a design perspective we must choose a different design. C# supports what is called "Multiple Implementation", which is to says a class can implement more than one interface. Our design now changes the "Vehicle" class and the "ConstructionVehicle" class into interfaces. Below we have defined the two interfaces with their very simplistic methods. i.e :

  1. interface IConstructionVehicle  
  2. {  
  3.     void ExecuteDump();  
  4.     void TurnOnBackUpSound();  
  5. }  
  6. interface IVehicle  
  7. {  
  8.     void Accelerate();  
  9.     void Stop();  
  10.     void TurnOnBackUpSound();  
  11. }  
If we built a class that inherited from these two interfaces we would be able to do so spanning multiple inherited interfaces. Design problem solved! 

Or is it?

Explicit Interface Implementation

If you look at both interfaces defined above you'll notice that they share in common a method of the same name "TurnOnBackUpSound()". Problem? No, in fact C# supports what is known as "Explicit Interface Implementation", which allows the programmer to specify which member of which interface they want to use. Putting the Interface name in front of the member name allows this to happen as shown below.

  1. public class DumpTruck : IEngine, IBody  
  2. {  
  3.     void IEngine.Test()  
  4.     {  
  5.         Console.WriteLine("This is the Engine TEst");  
  6.     }  
  7.     void IBody.Test()  
  8.     {  
  9.         Console.WriteLine("This is the Body TEst");  
  10.     }  
  11. }  
Implementation Hiding

Another benefit to this technique is something called "Implementation Hiding". Implementation Hiding allows the methods from the implemented interface to be hidden from the derived class unless the developer explicitly calls the interface. This technique obviously reduces the clutter for a developer. 

Further recommended readings on C# inheritance 
 
Here are more articles on inheritance and object oriented programming in C#.
 
Types of Inheritance in C# 
In this article, we will learn about C# inheritance and types of inheritance in C# and .NET with code examples.

Overview of Inheritance in C#
In this article, you will learn about inheritance in C#. Inheritance is one of the major pillars of object-oriented programming. Here is an overview of inheritance.

Inheritance with Example in C#
Here are some example of inheritance using C#.
 
Learn all about Inheritance in C#
In this article, you will learn about Inheritance in C#.
 
Playing with inheritance in C#
This article explains the various behaviors of inheritance in various cases.
 
Inheritance examples in C#
This article explains inheritance with examples in C#.
 
Multiple Inheritance in C#
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, will a Class fully represent an object?
 
Multiple Inheritance in C# using Interfaces
This is the simple mathematical operation program demonstrating how multiple inheritance can be achieved in C# using Interface Concept.
 
Inheritance in C#
Interface inheritance defines a new interface in terms of one or more existing interfaces. Implementation inheritance defines a new implementation in terms of one or more existing implementations.