Site icon TAAGUNG

Implement Multiple Inheritance in C#

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

After reading the title of this article, first thought comes in our head that C# doesn’t allow Multiple Inheritance. But there are few languages, which allows this, so let us first investigate why C# and Java don’t allow multiple Inheritance.

Multiple Inheritance – Why is it not allowed?

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

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

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

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

Implement Multiple Inheritance

But, in our real life, we can get into the situation when 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.  

Here is 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 previous Approach, we see that combining both of the classes could be a big headache and here we have second approach to implement the same –

In this approach, we have class ParentB implemented an Interface IParentB (@Line#17). We need to make sure that 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

   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 of the scenario, we see this output –

Output from both programs

When to use which approach?

Approach #1,
Approach # 2,

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

Source Code is available here for experiment.

Exit mobile version