Why multiple inheritance is not possible in c#
Rajesh Singh
It causes the ambiguity error in C#.
due to the diamond problem.
C# compiler is designed not to support multiple inheritence because it causes ambiguity of methods from different base class.
Basically it is due to Diamond Problem. example expalined below:public class A {public virtual void A_Method(){Console.WriteLine("Class A Method");} }public class B:A {public override void A_Method(){Console.WriteLine("Class B Method");} }public class C:A {public override void A_Method(){Console.WriteLine("Class C Method");} } public class D:B,C // If Multiple inheritence is possible in C# then {public override void A_Method(){Console.WriteLine("Class C Method");} }public static void main() { D objD = new D(); objD.A_Method();// Making object of class D and calling overloaded method A_method will confuse the compiler which class method to call as both inherited class methods has been overloaded. } It is called Diamond problem because of the structure it makes while inheriting the classes as below:Class A(Base Class)Class B(Inherits A) Class C(Inherits A)Class D(Inherits B, C)public class D : IA,IB // Multiple interfaces can be implemented but it will show only one methhods after inheriting for implementation{public void Show(){throw new NotImplementedException();}}interface IA{void Show();}interface IB{void Show();}
This is Cause by diamond Shape problems of two classes If two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C? So., multiple inheritance is not possible in C#. that is called Diamond Problem. But., in C# we can solve the Diamond problem with the help of interfaces in some cases.
http://www.c-sharpcorner.com/article/diamond-problem-in-c-sharp/
Due to Diamond problem....
Multiple inheritance does not get implemented in C# because of Diamond drawback.If it is required to implement multiple inheritance in C# then we use at least one Interface as base class.
Multiple inheritance is possible in c# while we will use atleast one base class as interface .
ambiguity issue....Class A{Public void show () { console.writeline("Show from class A"); } }Class B{Public void show (){console.writeline("show method from class b");}}Class C: class A, class B{ // which method call here..... //class A have a show method and class b have also same method so ambiguity problem is here};
Yes, it is due to the diamond problem.The diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C?
Yes, due to diamond problem.The diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C?
Simply, it will increase the complexity (the developer may difficult to find which method is from which class).
Because when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C? So., multiple inheritance is not possible in C#. that is called Diamond Problem.But., in C# we can solve the Diamond problem with the help of interfaces in some cases.
Due To Diamond Problem...
Due to diamond problem.
If multiple inheritance would have allowed in the C#, there would be a problem so called "Diamond Problem". Diamond problem occurs when same method exist in two or more base classes. Thus the child class would end up inheriting duplicate methods.
Multiple inheritance is not possible beacuse Multiple inheritance Suffer From Ambugity Problem,that means there can be chances that same method present in both the call.
http://net-informations.com/faq/general/inheritance.htm