Skip to content
March 10, 2008 / kiranpatils

Why C# doesn’t supports multiple inheritance?

Its good to know that  C# Supports Multiple Inheritance . But i  wrote  my  title that it  doesn’t. Yes  it doesn’t support by Implementation inheritance but supports using Interface  Implementation.  Sounds confused??  have a look at it…

Basic Inheritance


Two types of Inheritance:
1.    Implementation Inheritance: in which class inherits one class and implement/override its methods and properties for example Control object in which System.Windows.Forms.Textbox,System.Windows.Forms.Button both inherits their self from control class. But provides different functionality
2.    Interface inheritance: in which a class inherits from Interface. For example IDisposable. It just inherits definition not implementation. Any type which does interface inheritance it means that it will provide defined functionality called as “Contract”.

Multiple Inheritance:
A class derives from more than one class it is called Multiple inheritance
Multiple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named StudentMusician to inherit from a class named Person, a class named Musician, and a class named Worker. This can be abbreviated StudentMusician : Person, Musician, Worker.
Ambiguities arise in multiple inheritance, as in the example above, if for instance the class Musician inherited from Person and Worker and the class Worker inherited from Person. There would then be the following rules:
StudentMusician: Person, Musician, Worker
Musician : Person, Worker
Worker: Person
If a compiler is looking at the class StudentMusician it needs to know whether it should join identical features together, or whether they should be separate features. For instance, it would make sense to join the “Age” features of Person together for StudentMusician. A person’s age doesn’t change if you consider them a Person, a Worker, or a Musician. It would, however, make sense to separate the feature “Name” in Person and Musician if they use a different stage name than their given name. The options of joining and separating are both valid in their own context and only the programmer knows which option is correct for the class they are designing.
Debate
There is debate as to whether multiple inheritance can be implemented simply and without ambiguity. It is often criticized for increased complexity and ambiguity, as well as versioning and maintenance problems it can cause (often summarized as the diamond problem).[1] Detractors also point out multiple inheritance implementation problems such as not being able to explicitly inherit from multiple classes and the order of inheritance changing class semantics. There are languages that address all technical issues of multiple inheritance, but the main debate remains whether implementing and using multiple inheritance is easier than using single inheritance and software design patterns.

Multiple Inheritance arises Diamond Problem

programming languages with multiple inheritance and knowledge organization, 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 it), and B and C have overridden that method differently, then via which class does it inherit: B, or C?

For example, a class Button inherits from both classes Rectangle (for appearance) and Mouse (for mouse events), and classes Rectangle and Mouse both inherit from the Object class. Now if the equals method is called for a Button object and there is no such method in the Button class but there is an over-ridden equals method in both Rectangle and Mouse, which method should be called?

It is called the “diamond problem” because of the shape of the class inheritance diagram in this situation. Class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape.

C# Supports Multiple Inheritances by Interfaces only

NOTE: lots of help got from wikipedia

Happy Inheritance!!



9 Comments

Leave a Comment
  1. Hamid / Dec 19 2012 9:44 am

    still some confusion like I have Class A with Add Method and Class B with Sud Method.Wants to inherit in Class C adding new method Mul. Still C# will not allow the Multiple Inheritances

    • kiranpatils / Mar 23 2013 7:09 pm

      Yes, it won’t. Because it will create confusion

  2. rajivkumar / Jan 18 2013 5:30 am

    In the second example ,if A class is not present can we inherit both the B & C classes. That means it supports multiple inheritance if A class is Absent.

    • kiranpatils / Mar 23 2013 7:08 pm

      No, we can’t!

  3. photography website templates / Apr 15 2013 7:15 am

    Hey there! I know this is kinda off topic however I’d figured I’d ask.

    Would you be interested in exchanging links or maybe guest authoring a blog article or vice-versa?

    My website addresses a lot of the same subjects as yours and I feel we could greatly benefit from each
    other. If you’re interested feel free to shoot me an email. I look forward to hearing from you! Wonderful blog by the way!

  4. walkme.com / Apr 18 2013 9:00 am

    Greetings! I know this is kinda off topic however I’d figured I’d ask.
    Would you be interested in trading links or
    maybe guest writing a blog article or vice-versa?
    My blog discusses a lot of the same subjects as yours and
    I believe we could greatly benefit from each other.
    If you are interested feel free to shoot me an e-mail. I look forward to hearing
    from you! Fantastic blog by the way!

  5. Right away I am going away to do my breakfast, later than having my breakfast coming yet again to read additional news.

  6. Laverne / May 1 2013 5:25 pm

    Greetings from Florida! I’m bored at work so I decided to check out your site on my iphone during lunch break. I love the knowledge you provide here and can’t wait to take a look when I get home.
    I’m shocked at how quick your blog loaded on my mobile .. I’m not
    even using WIFI, just 3G .. Anyhow, very good blog!

    • kiranpatils / May 1 2013 6:10 pm

      Hello Laverne,

      Thank you for the appreciation, it matters a lot! It amazes me to know that, can present information in such a way that it throws away boredom as well!🙂

      Keep visiting! Keep reading! Keep sharing!

      Sincerely.
      Kiran Patil

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: