why multiple inheritance of classes in not supported in C#?

Category: c# language

Question

Shailesh Rathi, Pune on Fri, 06 Oct 2006 09:52:11


Multiple inheritance is not supported in C# but it can be achieved using interfaces is fine. But can you explain why multiple inheritance of classes in not supported in C#?

Replies

rauhanlinnake on Fri, 06 Oct 2006 10:11:23


Multiple inheritance causes problems, if one or more of the parent classes defines members with same identifier. You would have to decide which parent to use to handle each method call, and the operation of class can be then very different than intended.

And usually, there is no situations when multiple inheritance is needed. "Good" classes usually maps to some phenomenom or object, and very few things are at the same time  based on two different things. Usually when people wish to do multiple inheritance (is-a), a "has-a" relation is needed, meaning that a class contains some objects.

Multiple inheritance is also highly discouraged in languages where it is supported (C++ etc.). Multiple inheritance is confusing at best.

DeanB61 on Fri, 06 Oct 2006 11:29:32


Put simply it isn't in because it hasn't been designed into the language. Are you saying that you would like it? That's a different question and I think the majority of people wouldn't want it. Anders once said about Delphi that he didn't put it in because it would slow down the speed of the compiler. Whatever, it's of no real use, so most people woudn't want it.

Charlie Calvert on Fri, 06 Oct 2006 23:43:58


There is no definitive answer to this question. It is a matter of taste.

There is the classic diamond problem encountered in multiple inheritance, in which class D inherits from both B and C, which both inherit from A.

  A
 / \
B   C
 \ /
  D

So which copy of A does D get? The one from B, the one from C? Both? This way various languages resolve this problems is discussed here:

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

It is informative, if a bit dizzying, to read the last sentence in the explanation of the way C++ resolves this issue.

Here is an interesting discussion of this topic:

http://www.artima.com/intv/dotnet.html

Search on "Multiple Inheritance" in this discussion to hear Anders discuss this subject:

http://msdn.microsoft.com/msdntv/transcripts/20040624csharpahTranscript.aspx

- Charlie

 

 

 

 

Peca55 on Sun, 08 Oct 2006 03:58:05


Other writers has shown to you some good points, why there is't multiple inheritance of classes. I add, that it is roughly achieved by inheriting from multiple interfaces, what is supported by C#.

Shailesh Rathi, Pune on Mon, 09 Oct 2006 05:28:00


its really nice answer like that many ques. come in my mind, like if oops concept we use and implement then why provide delegate.and how oops concept helps.

csalsa on Mon, 09 Oct 2006 05:37:47


When I discuss multiple inheritance, I use (my) terms of 'open' and 'closed' inheritance.

I define 'open' as multiple inheritance is where there is no two or more paths to a common base class and 'closed' as where there is more than one path to a shared base type.  I do not know if there are official terms for my home-grown terms

Personally, I do not like 'closed' inheritance but I think there is some merit for 'open' inheritance.  However, I would agree with the previous posts that I have seen more bad uses of multiple inheritance than good uses.



rauhanlinnake on Tue, 10 Oct 2006 11:29:56


"come in my mind, like if oops concept we use and implement then why provide delegate."

I don't see why delegates would be violating against OOP principles. Delegates are not present in all OOP languages, but there are many things in .NET and vice versa that aren't. Delegates and especially their application events are very useful when building highly reusable and easy-to-use software components. Programmer can or must just implement the event handlers, and set some properties to get a module working. I have begun to use lots of events and delegates in my classes, even if they aren't components in .NET terminology (derived from System.Component). That really helps me to keep the program maintainable and modifiable.

"
and how oops concept helps"

People understand programs better if they contain structures that resembles to real life concepts, objects or phenomena. Human being is willing to forget the details, and concentrate on main concepts. OOP enables this, by defining classes and creating objects the programmer can raise the level of abstraction so, that he/she can (sometimes;)) forget the inner life of a module, and concentrate using it with other modules, to create "living and breathing" software.

EMoscosoCam on Sat, 15 Dec 2007 21:13:12


Of course, multiple inheritance could be problematic if you are not careful when programming. But I'd rather have that depending on me, not because the underlying technology does not allow me.

AsadHussain on Tue, 24 Aug 2010 12:17:29


Multiple inheritance is not supported in C# through classes because

Class A

{

public void func1()

{

.....;

}

}

 

Class B

{

public void func1()

{

....;

}

}

 

Class C:A,B

{

static void Main()

{

C obj=new C();

C.func1();//Here runtime gets confused whether to call func1() of Class A or B; Ambuguity

}

}

 

Where as with interfaces we have Implicit Interface Implementation

interface A

{

public void func1();

 

}

 

interface B

{

public void func1();

 

}

 

Class C:A,B

{

void A.func1()//Implicit interface implementation

{

...;

}

void B.func1()//Implicit interface implementation

{

...;

}

static void Main()

{

C obj1=new C();

A obj2=(A)obj1;

B obj3=(B)obj1;

obj2.func1();//Calls interface A's func1() method

obj2.func1();//Calls interface B's func1() method

}

}

}

 

Hence Multiple inheritance is possible using interfaces

Jason Robertson on Thu, 27 Sep 2012 22:26:15


I want it in the language. The reason being if you are generating code it adds extra flexibility there is so much in C# that inexperienced developers should not be let loose with that how is this going to be any different. The problem in code generation is you may not want to write a rule to generate everything you may just want to inherit a prepared class in some circustances not generate it in every circumstance and then have to write a rule for it.