HI WELCOME TO SIRIS

Why C# does not support multiple class inheritance?

Leave a Comment

Why C# does not support multiple class inheritance?

or
What are the problems of multiple class inheritance?

C# does not support multiple class inheritance because of the diamond problem that is associated, with multiple class inheritance. Let us understand the diamond problem of multiple class inheritance with an example.

As shown in the image above:
1. I have 2 classes - ClassB and ClassC
2. Both of these classes inherit from ClassA
3. Now, we have another class, ClassD which inherits from both ClassB and ClassC

So, if a method in ClassD calls a method defined in ClassA and ClassD has not overriden the invoked method. But both ClassB and ClassC have overridden the same method differently. Now, the ambiguity is, from which class does, ClassD inherit the invoked method: ClassB, or ClassC?

In order not to have these problems, C# does not support multiple class inheritance.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.