Tuesday 25 September 2012

Why C# does not support multiple inheritance?

This is question most of the people ask every time. So I thought it will be good to write a blog post about it. So why it does not support multiple inheritance?

I tried to dig into the problem and I have found the some of good links from C# team from Microsoft for why it’s not supported in it. Following is a link for it.

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/07/85562.aspx

Also, I was giving some of the example to my friend Dharmendra where multiple inheritance can be a problem.The problem is called the diamond problem. Let me explain a bit. If you have class that is inherited from the more then one classes and If two classes have same signature function then for child class object, It is impossible to call specific parent class method.

Here is the link that explains more about diamond problem.

http://en.wikipedia.org/wiki/Diamond_problem

Now of some of people could ask me then why its supporting same implementation with the interfaces. But for interface you can call that method explicitly that this is the method for the first interface and this the method for second interface. This is not possible with multiple inheritance. Following is a example how we can implement the multiple interface to a class and call the explicit method for particular interface.

Multiple Inheritance in C#

That’s it. Hope you like it. Stay tuned for more update..Till then happy programming.

No comments :