You must Sign In to post a response.
  • Category: .NET

    Why csharp does not support multiple inheritance?

    Hi Friends,I know multiple inheritance can be achieved using interfaces.I just want to know why c sharp does not support?I got this question in interview
  • #693190
    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 the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?

  • #693191
    because they designed c# it in that way.
    to reduce the complexity related to multiple "implementation inheritance".

    see the following links

    http://stackoverflow.com/questions/995255/why-is-multiple-inheritance-not-allowed-in-java-or-c

    also see the diamond problem here

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

    Regards,
    Shine

  • #693195
    it wil not support bcoz of if a class is inherted from more than one class & if 2 classes hv same signture function then for child class obj, it's impossible to call specific parent class method and this problem is
    called daimand problm, for this solution we using multiple interfaces

  • #693199
    The C# will get a Diamond error if the same method is present in multiple class thus to over come this issue it does not support it directly.

    But indirectly you can achieve this using the interfaces where multiple interfaces can be used to be inherited in a child class.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #693202
    Take reverse case, let's say C# supports multiple inheritance. now we see below explanaiton where you will get to know, "why C# not provide multipel inheritance"


    class first
    {
    protected void testMethod();
    }

    class second
    {
    protected void testMethod();
    }


    class third: first, second
    {

    }


    class Void Main
    {
    third objThird = new third();
    objThird.testMethod(); //Ooops! Which method to call now? A's or B's ?
    }



    from above method you will get to know that program get's conflicts in calling method

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.