Why multiple inheritance is not supported in c#?

Forums C#Why multiple inheritance is not supported in c#?
Staff asked 3 years ago

Why multiple inheritance is not supported in c#?

Answers (1)

Add Answer
Shaikh Hafeezjaha Marked As Accepted
Staff answered 3 years ago

Suppose the inheritance is possible in c#

public class A
       {
           public virtual void Method_A()
           {
               Console.WriteLine("Class A Method");
           }
       }
       public class B : A
       {
           public override void Method_A()
           {
               Console.WriteLine("Class B Method");
           }
       }
       public class C : A
       {
           public override void Method_A()
           {
               Console.WriteLine("Class C Method");
           }
       }
       public class D : B, C
       {
           public override void Method_A()
           {
               Console.WriteLine("Class C Method");
           }
       }
       public static void main()
       {
           D objD = new D(); objD.Method_A();
       }

When an object of class D calls the overloaded method Method_A will confuse the compiler on which class method to call as both inherited class methods have been overloaded.

It is called the Diamond problem.

So there occurs the ambiguity problem while invoking the methods. To solve this problem, C# does not support multiple inheritances.

Subscribe

Select Categories