2

Yes I was asked this question and all I could provide, was a summary of following info that I knew. Ofcourse other than getting the error when writing the so-called expected code, wish we could write 1, could there be more valid reasons to prove that C# doesn't support Multiple Inheritance?

Many questions asked here in SO and outside web in the context of MI of C#, comparing C# to Java (where Java implements MI with interfaces), how to simulate/emulate MI in C#, How to deal with lack of MI in C#, what is the problem with MI etc.. So my question may reflect bits of duplicity to those questions...however I haven't found the answer yet, hence thought of shooting.

In a blog I saw the author is talking about some valuable points:

  1. Different implementations of MI across languages makes it a challenge to make a language-neutral implementation.
  2. Interfaces can be used instead, which makes MI a bit redundant.
  3. MI adds complexity in regards to casting, reflection and so on.

Author indicates that Multiple inheritance of implementation is what not allowed in C#. So to stop leading to a double diamond inheritance problem/deadly diamond of death.. Like author had pointed out saying "I have inherited a lot from both my mother and father"..

So it makes more sense for a Class to inherit from multiple super/base/parent classes. E.g. Class House inherits from Class Building (the noun, represents details of a building{house, school, office, hotel etc}) and Class Construction (represents details of construction a building). Thus it would be great if we could,

1. code wish we could write
public class House: Building, Construction {
// methods, properties...
} 

Instead we have to write that interFACE..

interface Buildstruction
{
    methodConstruction();
}
//class Construction then has to implement the interface

public class  House: Building, Buildstruction
{
    Construction ConstionObject;
    methodBuilding();
    methodConstruction() { ConstionObject.methodConstruction(); }
}

Wouldn't it had been easier to not allow multiple super classes to have the same name methods (e.g. calculate method)? Then I realize those parent/super classes need to have the freedom to create their own properties and methods independent of another class..

Thus MI seems mission impossible in C#...and what reasons determined why it is so.. Is it merely because of,

  • The diamond problem?
  • And no-body has yet really pointed out the real use of MI in programming?

Also how does,

  • MI add complexity in regards to casting, reflection?
  • Interfaces make MI a bit redundant?
  • MI cause issues for a code (full OOP supported) to be language independent?

Appreciate a comment/an answer that says more than just "it's the way C# language is created..." :)

4

0 回答 0