Dot Net For All

Why C# doesn’t support multiple inheritance?

Why C# doesn’t support multiple inheritance?

I have started writing my new article about the Interfaces in C#. But as soon as we read about interfaces, the first thing everyone mentions is that C# does not support multiple inheritance(MI) rather it supports MI but in its downgraded version viz. MI is supported in .NET framework using interfaces which I have discussed in my next article. Why C# doesn’t support multiple inheritance ? I thought let’s first write about the logic behind not supporting MI in .NET using classes.

Multiple inheritance is form of inheritance where two or more classes are inherited by the derived class

So why doesn’t C# or .NET support MI.

Using my study for the article on the internet I have found out that there is varied number of reason for not supporting MI. First of all MI in C++ has been a big mess but for some obvious reason which I want to discuss here, due to this the creators of .NET framework didn’t want to carry the same confusion which MI created in C++.

Though I am not a C++ developer and I would not hesitate to admit that I am very poor in pointers and all the C++ stuff.

Basically there have been mainly two reasons which .NET framework developers must have thought before coming to conclusion of removing MI support from .NET framework.

  1. Diamond Shape Problem –

Suppose we have a class A which defines a virtual method named MyTask(), and A class is turn inherited by classes B and C.  Both B and C have overridden the base class MyTask() function . Till now the scenario is fine and it looks good to have this kind of structure but suppose a need comes and we have to create a new class which in turn derived from B and C, so the code looks like as shown below (though it would give a compile time error in C#)

Same I would like to explain using the following code example

public class Employee
    {
        public virtual void DoWork()
        {
            //Do work
        }
    }

    public class Manager:Employee
    {
        public override void DoWork()
        {
            //Manager's implementation of do work
        }
    }

    public class Developer : Employee
    {
        public override void DoWork()
        {
            //Deveoper's implementation of do work
        }
    }

    public class JuniorDeveloper:Manager, Developer// Compile Time Error :Cannot have multiple base classes but assume that it were possible
    {

    }

Now suppose if I create an variable of type Employee having an instance of type junior developer and call DoWork function as following. Junior developer would be confused whose work to do.

Employee emp = new JuniorDeveloper();
emp.DoWork();

2. Another case is also somewhat similar to the previous one but I would like to discuss here. Suppose in JuniorDeveloper’s constructor I want to call the base class constructor using the base keyword as shown below.

public class JuniorDeveloper:Manager, Developer// Compile Time Error :Cannot have multiple base classes but assume that  it were possible
    {
        public JuniorDeveloper():base()
        {

        }
    }

In the above scenario also the constructor of Junior developer would be in confusion to call which base class constructor as it derives from two classes.

Important Points:

MI is not supported in .NET because:

  1. It is hard to inderstand.
  2. It can inducece unpredictable bugs.
  3. It is difficult to debug and understand.

These are primarily two of the cases considering which MI support in .NET framework has been removed. Please let me know your thoughts about the article and feel free to add from your side if I am missing something here.

Further Reading :

    1. Interface in C#
  1. Why do we use interface in C#

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview