locked
Why multiple Inheritance not support in c# ? RRS feed

  • Question

  • Why multiple Inheritance not support in c# ?

     


    Kalai
    Friday, August 5, 2011 5:03 AM

Answers

  • To avoid disadvantages of it. COnsider the below simple example (assuming if multiple inheritance is supported)

    class A
    {
        protected void SampleMethod();
    }
    
    class A
    {
        protected void SampleMethod();
    }
    
    
    class C: A, B
    {
    
    }
    
    
    class MainClass
    {
        new C().SampleMethod(); //Ooops! Which method to call now? A's or B's ?
    }
    

    In the above example, Since C inherits both A & B, there will to SampleMethod() in C. When you try to call SampleMethod on object oc, there will conflict between which method to call. Isn't it?

    This should be enough explanation to make you understand why multiple inheritance is not supprted in .NET


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by RohitArora Friday, August 5, 2011 6:46 AM
    • Marked as answer by Paul Zhou Thursday, August 18, 2011 5:28 AM
    Friday, August 5, 2011 5:29 AM
    • Proposed as answer by pvdg42 Friday, August 5, 2011 4:14 PM
    • Marked as answer by Paul Zhou Thursday, August 18, 2011 5:28 AM
    Friday, August 5, 2011 5:44 AM

All replies

  • To avoid disadvantages of it. COnsider the below simple example (assuming if multiple inheritance is supported)

    class A
    {
        protected void SampleMethod();
    }
    
    class A
    {
        protected void SampleMethod();
    }
    
    
    class C: A, B
    {
    
    }
    
    
    class MainClass
    {
        new C().SampleMethod(); //Ooops! Which method to call now? A's or B's ?
    }
    

    In the above example, Since C inherits both A & B, there will to SampleMethod() in C. When you try to call SampleMethod on object oc, there will conflict between which method to call. Isn't it?

    This should be enough explanation to make you understand why multiple inheritance is not supprted in .NET


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Proposed as answer by RohitArora Friday, August 5, 2011 6:46 AM
    • Marked as answer by Paul Zhou Thursday, August 18, 2011 5:28 AM
    Friday, August 5, 2011 5:29 AM
    • Proposed as answer by pvdg42 Friday, August 5, 2011 4:14 PM
    • Marked as answer by Paul Zhou Thursday, August 18, 2011 5:28 AM
    Friday, August 5, 2011 5:44 AM
  • To avoid disadvantages of it. COnsider the below simple example (assuming if multiple inheritance is supported)

    class A
    {
      protected void SampleMethod();
    }
    
    class A
    {
      protected void SampleMethod();
    }
    
    
    class C: A, B
    {
    
    }
    
    
    class MainClass
    {
      new C().SampleMethod(); //Ooops! Which method to call now? A's or B's ?
    }
    

    In the above example, Since C inherits both A & B, there will to SampleMethod() in C. When you try to call SampleMethod on object oc, there will conflict between which method to call. Isn't it?

    This should be enough explanation to make you understand why multiple inheritance is not supprted in .NET


    Please mark this post as answer if it solved your problem. Happy Programming!

    Advadesh, thats not the reason why multiple inheritance is supported. If you can think out of the box, the same scenario can occur when you multiple interfaces with same method signature implemented on a same class. .Net compiler intelligently can identify using the Interface name.
    Tuesday, August 9, 2011 5:01 AM
  • @jebarson - When you are implementing multiple intefaces containing same method, you must explicitly prefix the interface right?

    interface A
    
    {
    
      void Method();
    
    }
    
    
    
    interface B
    
    {
    
      void Method();
    
    }
    
    
    
    class C: A, B
    
    {
    
      public void A.Method()
    
      {
    
      }
    
    
    
      public void B.Method()
    
      {
    
      }
    
    } 
    
    


    Does it make sense? Do you think it is multiple inheritance?


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Edited by Adavesh Thursday, August 11, 2011 3:28 AM
    Tuesday, August 9, 2011 5:25 AM
  • advadesh, if you can read my answer again, i have mentioned that the method identification is not the reason behind multiple inheritance being supported. As the same way you have mentioned above, the compiler can identify the method if it needs to.
    Tuesday, August 9, 2011 6:06 AM
  • Yeah.. May be I overlooked your statement. And yes, that is what the nature of interfaces right.
    Please mark this post as answer if it solved your problem. Happy Programming!
    Tuesday, August 9, 2011 6:20 AM
  • I thought Multiple inheritance is achievable via the interfaces, in C# multiple inheritance is not possible via classes agree, but interfaces can be used to provide for multiple inheritance.
    Abhinav
    Tuesday, August 9, 2011 6:36 AM
  • u have used both class name as A how come A,B inherited to C 


    Kalai
    Thursday, August 11, 2011 2:40 AM
  • 'C' got missed there. Corrected that 
    Please mark this post as answer if it solved your problem. Happy Programming!

    Thursday, August 11, 2011 3:28 AM
  • Hey this explains neatly but i have a question. Why and how come, then its being supported in C++? I know this forum topic is off the C++  but just curious to ask here itself.

    Aravind

    Tuesday, June 12, 2012 8:16 AM
  • C++ originated before C#. In C++ it is definitely an issue. C# team understood the potential problem with multiple inheritance and hence they removed support for multiple inheritance.

    Please mark this post as answer if it solved your problem. Happy Programming!

    Tuesday, June 12, 2012 11:31 AM
  • Thanks Sir ,The Best solution of this problem is ever seen....
    Tuesday, October 30, 2012 6:58 AM