Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Multiple inheritance using interfaces as mentioned below is possible to achieve both the properties of A and B classes to AB class.

interface IA
  { void Print(); }
  interface IB
  { void Print(); }

  class A : IA
  {
      public void Print()
      { Console.WriteLine("ClassA"); }
  }

  class B : IB
  {
      public void Print()
      { Console.WriteLine("ClassB"); }
  }

  class AB : IA, IB
  {
      void IA.Print()
      {
          A a = new A();
          a.Print();
      }

      void IB.Print()
      {
          B b = new B();
          b.Print();
      }
  }

  class Test
  {
      public void test()
      {
          AB ab = new AB();
          ((IA)ab).Print();
          ((IB)ab).Print();
      }
  }



Then why cant we have multiple class inheritance as mentioned below.

class A
{
    public void Print()
    { Console.WriteLine("ClassA"); }
}

class B
{
    public void Print()
    { Console.WriteLine("ClassB"); }
}

class AB : A, B
{

}

class Test
{
    public void test()
    {
        AB ab = new AB();
        ((A)ab).Print();
        ((B)ab).Print();
    }
}



As far i know C# wont support multiple class inheritance because it can not decide which method to call when both the base classes are having same method signature.

Then it can be overcome by having code something like above.

I am aware of the diamond problem which also can solve similar to above way.


am i missing to understand something here?
Or it is just like C# doesn't implement this kind of functionality without any reason?

What I have tried:

I tried to understand by reading the available articles in internet
Posted
Updated 29-Mar-18 9:33am

First off, Interfaces don't provide multiple inheritance: interfaces are not classes, they are a contract, and you can't "inherit" from an interface.

As to why C# doesn't support MI, the most likely reasons is "because the benefits do not overweigh the complexity involved in implementing it" - and the main reason for that complexity is the Diamond Problem: Multiple inheritance - Wikipedia[^]

The diamond problem arises when two classes B and C inherit from A, and another class D also inherits from both B and C. If a method in D calls a method defined in A, and B and C have overridden that method, then which class method should be executed? the version from B, or the version from C?

Although it is possible to get round this (C++ does) it complicates the language and the code you write - and C# is designed to encourage clean, simple, and maintainable code (an accusation not often leveled at C++ code!).
 
Share this answer
 
Unfortunately C# does not support multiple inheritance. You can read more from Why doesn’t C# support multiple inheritance? | C# Frequently Asked Questions[^]

However, depending on the situation there are ways to overcome this restriction. For example in many cases extending a class with extension method does the trick. Have a look at Extension Methods (C# Programming Guide) | Microsoft Docs[^]
 
Share this answer
 
Because it is a toy language doesn't support it.
 
Share this answer
 
Comments
Wendelius 29-Mar-18 15:44pm    
Biased, are we? :)
CPallini 29-Mar-18 15:59pm    
Who, me? :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900