Implement Multiple Inheritance In C#

In this article, you will learn how to implement multiple inheritance in C#.

After reading the title of this article, the first thing that gets clarified in our head is that C# doesn’t allow Multiple Inheritance. However, there are a few languages that allow this. Let us first investigate why C# and Java don’t allow multiple Inheritance. 

Multiple Inheritance - Why is it not allowed?

 
Implement Multiple Inheritance In C# 

Considering the above hypothetical class diagram. We want to have a class Child which inherits Parent classes ParentA and ParentB. Both parent classes have the same methods, MethodA and MethodB.

Now, when we instantiate the Child class, then calling MethodA will confuse the compiler regarding from which class MethodA should be called.

The similar case will be observed when both classes (ParentA and ParentB) inherit a SuperClass as shown here.

Implement Multiple Inheritance In C# 

Since this structure resembles a diamond, this problem is famous as the Diamond Problem (See the similarity between the class diagram and diamond shape) and we often hear “Because of the Diamond Problem, languages don’t allow multiple inheritance.” 

Implementing Multiple Inheritance

 
In real life, we can get into a situation where we need to implement multiple inheritance. So, let us see the workarounds to achieve this.
 
Approach #1

In this approach, we make a wrapper class, ParentWrapper, and have methods from both of the classes. This is a way to combine the classes.

Implement Multiple Inheritance In C# 

Here is the code implementation of the approach.

  1. class ParentA  
  2.   {  
  3.       public void MethodA()  
  4.       {  
  5.           Console.WriteLine("MethodA from ParentA called");  
  6.       }  
  7.       public void MethodB()  
  8.       {  
  9.           Console.WriteLine("MethodB from ParentA called");  
  10.       }  
  11.   }  
  12.   
  13.   class ParentB  
  14.   {  
  15.       public void MethodA()  
  16.       {  
  17.           Console.WriteLine("MethodA from ParentB called");  
  18.       }  
  19.       public void MethodB()  
  20.       {  
  21.           Console.WriteLine("MethodB from ParentB called");  
  22.       }  
  23.   }  
  24.   
  25.   class ParentWrapper  
  26.   {  
  27.       ParentA objA = new ParentA();  
  28.       ParentB objB = new ParentB();  
  29.       public void ParentWrapperAMethodA()  
  30.       {  
  31.           objA.MethodA();  
  32.       }  
  33.       public void ParentWrapperAMethodB()  
  34.       {  
  35.           objA.MethodB();  
  36.       }  
  37.       public void ParentWrapperBMethodA()  
  38.       {  
  39.           objB.MethodA();  
  40.       }  
  41.       public void ParentWrapperBMethodB()  
  42.       {  
  43.           objB.MethodB();  
  44.       }  
  45.   }  
  46.   
  47.   class Child : ParentWrapper  
  48.   {  
  49.   
  50.   }  

This is how we can call them.

  1. Child objChild = new Child();  
  2. objChild.ParentWrapperAMethodA();  
  3. objChild.ParentWrapperBMethodB();  

Approach #2

In the previous approach, we see that combining both the classes could be a big headache and here, we have the second approach to implement the same.

Implement Multiple Inheritance In C# 

In this approach, we have a class ParentB implemented an Interface IParentB (@Line#17). We need to make sure that the interface IParentB has all methods defined in ParentB class (From Line# 19-22). Now, our class Child will inherit Class ParentA and implement Interface IParentB (@Line#32). Since class Child is implementing IParentB interface, so we will have to implement all the methods of this in Child class and we can have it referencing ParentB (From Line#35-38).

Code implementation of this approach will be like this.

  1. class ParentA  
  2.  {  
  3.      public void MethodX()  
  4.      {  
  5.          Console.WriteLine("MethodX from ParentA called");  
  6.      }  
  7.      public void MethodY()  
  8.      {  
  9.          Console.WriteLine("MethodY from ParentA called");  
  10.      }  
  11.      public void MethodZ()  
  12.      {  
  13.          Console.WriteLine("MethodZ from ParentA called");  
  14.      }  
  15.  }  
  16.   
  17.  class ParentB : IParentB  
  18.  {  
  19.      public void MethodA()  
  20.      {  
  21.          Console.WriteLine("MethodA from ParentB called");  
  22.      }  
  23.      
  24.  }  
  25.   
  26.  interface IParentB  
  27.  {  
  28.      void MethodA();  
  29.      
  30.  }  
  31.   
  32.  class Child : ParentA, IParentB  
  33.  {  
  34.      ParentB objParentB = new ParentB();  
  35.      public void MethodA()  
  36.      {  
  37.          objParentB.MethodA();  
  38.      }  
  39.  }  

 And here, we can call it.

  1. Child objChild2 = new Child();  
  2. objChild2.MethodX();  
  3. objChild2.MethodA();  

Output

In both the scenarios, we see this output.

Implement Multiple Inheritance In C# 
 

When to use which approach?

 
Approach #1
  • It will be good when the class size is small and it doesn’t have too many methods.
  • We are just provided classes and can NOT modify them at all.
  • All constituent classes have the same method name.
Approach # 2,
  • This approach is best suited when at least one class could be modified.
  • When one class has a smaller number of methods than another class.
  • When methods are different in all classes.

I hope, with this, I could explain the reason for not having Multiple inheritance. If extremely needed, I have explained the ways to implement this along with the best evaluation of the best approach in both of the scenario.

Source Code to experiment with the concepts explained in this article is available here.