Why multiple Inheritance is not possible in C#?

(Please do not answer like this-It is possible through Interfaces.)

Questions by Rohit Sharma   answers by Rohit Sharma

Showing Answers 1 - 24 of 24 Answers

Sunil

  • Apr 28th, 2006
 

When we use the Multiple inherutance .. we use more than one class.....ok. Lets one condition class A and class B are base classes and class c is is multiple inherting it ok........and where class c is inheriting a function ......ok it may be possible that this function with same name and same signature can present in both class A and Class B .. That time how the compiler will know ..that which function it should take wherether from class A or class B.So this time Multiple inheritanc won't work .So avoiding this problem we use Interface..... what is interface ... means in interface we just declare a function ...ok and in the derived class we give the definition as per the requirement...means function should be abstract ... and in interface all funcion[method] should abstract.

  Was this answer useful?  Yes

Hasham

  • Apr 28th, 2006
 

To avoid name collision. Multiple Inheritence can have many classes and methods , and its quite possible that they can have save name " say a save function in two different classe name can be Save() but implementation can be different". The derived class will have problem accessing the correct function.

  Was this answer useful?  Yes

Rohit Sharma

  • May 2nd, 2006
 

Then how it was working fine in C++??

  Was this answer useful?  Yes

NageshwaraRao

  • May 8th, 2006
 

Hi Dear Friends..!

Multiple Inheritance is possible in C++, but its not possible in Java, and C#.

what we are read above those are all we know, but as per my knowledge, in C++ to resolve multiple inheritance we are using "this pointer". But in C# there is no pointer concept. In such way, the architectures using the interface concept. To resolving the ambiguity "this pointer" concept used in c++.

Offcourse it not absolutely correct, but its my idea only. If you are agree with my view then follow it, else leave it.

  Was this answer useful?  Yes

Rajsekhar

  • Sep 1st, 2006
 

Hi

  Your answer is good but I have doubt, I can call the Method is class as A.fun1(), B.fun1() in derived class C. In that way we can use the multiple inheritance concept in dotnet.  

  Was this answer useful?  Yes

jonathan

  • Sep 17th, 2006
 

Multple inheritance is coneceptually wrong. It shouldn't be allowed in any language. Inheritance is the strongest relationship that can be expressed in OO languages. It's used to express IS-A relationship. Aggregation is used to express IS CONSTRUCTED IN TERMS OF. If you're using multiple inheritance in C++ then you're design is wrong and you probably want to use aggregation. On the other hand it's plausible to want to use multiple interfaces. For instance you might have a class wheel and a class engine. You could say that your class car inherits from wheel and from engine but that's wrong. In fact car aggregates wheel and engine because it is built in terms of those classes. If wheel is an interface and engine is an interface then car must inherit both of these interfaces since it must implement the functionaity of wheel and engine .On this basis we can see that multiple inheritance for classes should not be allowed because it promotes mis-use of the strong IS-A relationship. C# enforces the correct concepts whilst C++ allows mis-use. multiple interface inheritance is permissible and C# allows this. It's all to do with properly understanding OO concepts.Try reading Exceptional C++ by Herb Sutter to get a good discussion of the correct and incorrect uses of inheritance.

  Was this answer useful?  Yes

Ravi Kumar

  • Mar 25th, 2007
 

Jonathan is right. Absolute Multiple Inheritance is not possible in c# but partially it supports multiple  inheritance  by the use of Interfaces. As interfaces force a class to implement same type of behaviour (as defined in interface) which classes implements that interface.

  Was this answer useful?  Yes

As one of my friend has specified that there is pointer concept in C++ and its not possible in C#. Even the pointers can be achieved through putting the same code in UnSafe.

According to me, Its not possible because inventors wanted to avoid the name collison in case if the derived class is inheriting two base classes and if the method names are the same  would result in collision..

This could be one of the reason I dont admit that this is the primary reason.

but this can be achived thru Interfaces

  Was this answer useful?  Yes

Ankit Seth

  • Feb 11th, 2011
 

I've seen all the answers mentioned here but nobody is specifically telling what is the real reason for OOPS language like JAVA,C# not implementing multiple inheritance.
According to me the real reason is the problem of "Identity".Suppose we're having a class Child deriving from Father and Mother.So a child cannot be Father and Mother both at the same time.So this is a problem of "Identity".We can create a interface IParent and Child can derive from Father or Mother and implement the behaviour of IParent. 

  Was this answer useful?  Yes

Ankit Seth

  • Jul 11th, 2011
 

I don't think that overriding will be a problem if you define your generic interface in a good manner.I've written that multiple inheritance is avoided to prevent the identity crisis. So it is the designer or developer responsibility to design the whole application in such a manner that such problem doesn't occur.

  Was this answer useful?  Yes

sajad

  • Jul 19th, 2011
 

True

http://msdn.microsoft.com/en-us/library/system.object.gethashcode.aspx

  Was this answer useful?  Yes

Riki

  • Sep 1st, 2015
 

C# does not allow multiple inheritance because of ambiguity. Say class A has a property "name" which is inherited by class X and Y now we have another class B which is inheriting both X and Y. Here is the problem Because B is also inherited from A so B is having a copy of "name" property but B is also inherited form X and Y and they are again inherited from A so each of them are having a copy of "Name". Now the problem is what does B have?? 1 copy from A or one copy from each X and Y. So thats ambiguous.
Now by using interface we can inherit type not behavior (implementation of multiple interfaces is also an inheritance of types). And there are some cases were we really need multiple inheritance and in C# we can get close to a simulation of multiple inheritance by using delegates.

  Was this answer useful?  Yes

Anujan

  • Dec 30th, 2015
 

I think that ambiguity can be easily overridden by using the class name as a prefix before the method. The same as we do as in case we want to implement a method with the same name as in the case of interfaces. The reason why we do not have multiple inheritance in C# is because Microsoft did want it. It could have been a possibility, but decided against it.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions