GeekInterview.com
Series: Subject: Topic:
Question: 124 of 460

Why multiple Inheritance is not possible in C#?

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

Asked by: Rohit Sharma | Member Since Apr-2006 | Asked on: Apr 21st, 2006

View all questions by Rohit Sharma   View all answers by Rohit Sharma

Showing Answers 1 - 12 of 12 Answers
Sunil

Answered On : 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.

  
Login to rate this answer.
Hasham

Answered On : 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.

  
Login to rate this answer.
Rohit Sharma

Answered On : May 2nd, 2006

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

  
Login to rate this answer.
NageshwaraRao

Answered On : 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.

  
Login to rate this answer.
Rajsekhar

Answered On : 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.  

  
Login to rate this answer.
jonathan

Answered On : 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.

  
Login to rate this answer.
Ravi Kumar

Answered On : 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.

  
Login to rate this answer.
Basavarajayya

Answered On : Nov 14th, 2009

View all answers by Basavarajayya

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

  
Login to rate this answer.
Ankit Seth

Answered On : Feb 11th, 2011

View all answers by Ankit Seth

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. 

  
Login to rate this answer.
thetheaye

Answered On : Jul 10th, 2011

View all answers by thetheaye

It can lead to collision in method overriding.

  
Login to rate this answer.
Ankit Seth

Answered On : Jul 11th, 2011

View all answers by Ankit Seth

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.

  
Login to rate this answer.
sajad

Answered On : Jul 19th, 2011

True

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.