Why C# does not support multiple class inheritance ?
Multiple inheritances are not supported in C#.
Consider the below example.
public class Class1 { public string GetClass() { return "This is Class1"; } } public class Parent2 { public string GetClass() { return "This is Class2"; } } public class SubClass: Class1, Class2 { public string GetCorrectClass() { return this.GetClass(); } }
Here there are two Parent class Class1 and Class2. This is inherited by Child class SubClass.
In GetCorrectClass method, child class calls the parent class method GetClass(). In such case which method will be executed?
It is very difficult for CLR to identify which method to call. It shows that multiple inheritance will create ambiguity to oops concept. In order to avoid this ambiguity we do not have multiple class inheritance and we have multiple interface implementations in C#.
No comments:
Post a Comment