Implement Multiple Inheritance In C#

Introduction

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.

Why Multiple Inheritance is not allowed?

Implement Multiple Inheritance

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 calling MethodA will confuse the compiler regarding from which class MethodA should be called.

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

Superclass

Since this structure resembles a diamond, this problem is famous as the Diamond Problem (See the similarity between the class diagram and the diamond shape) and we often hear &ldquo that 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.

implementing multiple inheritance

Here is the code implementation of the approach.

class ParentA
{
    public void MethodA()
    {
        Console.WriteLine("MethodA from ParentA called");
    }
    public void MethodB()
    {
        Console.WriteLine("MethodB from ParentA called");
    }
}
class ParentB
{
    public void MethodA()
    {
        Console.WriteLine("MethodA from ParentB called");
    }
    public void MethodB()
    {
        Console.WriteLine("MethodB from ParentB called");
    }
}
class ParentWrapper
{
    ParentA objA = new ParentA();
    ParentB objB = new ParentB();
    public void ParentWrapperAMethodA()
    {
        objA.MethodA();
    }
    public void ParentWrapperAMethodB()
    {
        objA.MethodB();
    }
    public void ParentWrapperBMethodA()
    {
        objB.MethodA();
    }
    public void ParentWrapperBMethodB()
    {
        objB.MethodB();
    }
}
class Child: ParentWrapper
{
    
}

This is how we can call them.

Child objChild = new Child();
objChild.ParentWrapperAMethodA();
objChild.ParentWrapperBMethodB();

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

Implement Multiple Inheritance

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 the ParentB class (From Lines # 19-22). Now, our class Child will inherit Class ParentA and implement Interface IParentB (@Line#32). Since class Child is implementing the IParentB interface, so we will have to implement all the methods of this in the;Child class, and we can have it referencing ParentB (From Line#35-38).

Code implementation of this approach will be like this.

class ParentA
{
    public void MethodX()
    {
        Console.WriteLine("MethodX from ParentA called");
    }
    public void MethodY()
    {
        Console.WriteLine("MethodY from ParentA called");
    }
    public void MethodZ()
    {
        Console.WriteLine("MethodZ from ParentA called");
    }
}
class ParentB : IParentB
{
    public void MethodA()
    {
        Console.WriteLine("MethodA from ParentB called");
    }
}
interface IParentB
{
    void MethodA();
}
class Child : ParentA, IParentB
{
    ParentB objParentB = new ParentB();
    public void MethodA()
    {
        objParentB.MethodA();
    }
}

And here, we can call it.

Child objChild2 = new Child();
objChild2.MethodX();
objChild2.MethodA();

Output. In both scenarios, we see this output.

Implement Multiple

When to use which approach?

Approach 1

  • It will be good when the class size is small and it does not 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 can 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 scenarios.

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


Similar Articles