Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How did C#'s lack of multiple inheritance lead to the need for interfaces?

In The C# Programming Language Krzysztof Cwalina states in an annotation:

we explicitly decided not to add support for multiple inheritance [...] the lack of multiple inheritance forced us to add the concept of interfaces, which in turn are responsible for problems with the evolution of the framework, deeper inheritance hierarchies, and many other problems.

Interfaces are a core concept to OO programming languages. I don't follow the meaning of "forced us to add the concept of interfaces"

Does Krzysztof mean that certain design decisions had to be made regarding the use of interfaces where otherwise mulitple inheritance would be used? Or, does he mean that interface's were introduced to C# because of a lack of multiple inheritance? Can you provide an example?

like image 817
P.Brian.Mackey Avatar asked Jan 23 '13 14:01

P.Brian.Mackey


People also ask

How did they make C?

The C language was actually created to move the UNIX kernel code from assembly to a higher level language, which would do the same tasks with fewer lines of code. Oracle database development started in 1977, and its code was rewritten from assembly to C in 1983. It became one of the most popular databases in the world.

When did C come out?

In 1978, Brian Kernighan and Dennis Ritchie published The C Programming Language, which would serve as the language reference until a formal standard was adopted.

Where did C come from?

C, computer programming language developed in the early 1970s by American computer scientist Dennis M. Ritchie at Bell Laboratories (formerly AT&T Bell Laboratories).

How did you learn C language?

Official C documentation - Might be hard to follow and understand for beginners. Visit official C Programming documentation. Write a lot of C programming code - The only way you can learn programming is by writing a lot of code.


1 Answers

An interface is simply a base class that has no data members and only defines public abstract methods. For example, this would be an interface in C++:

class IFrobbable {     public:     virtual void Frob() = 0; } 

Therefore when MI is available as a language feature you can "implement" interfaces by simply deriving from them (again, C++):

class Widget : public IFrobbable, public IBrappable {     // ... } 

Multiple inheritance in the general case gives rise to many questions and problems that don't necessarily have a single answer, or even a good one for your particular definition of "good" (dreaded diamond, anyone?). Multiple interface implementation sidesteps most of these problems exactly because the concept of "inheriting" an interface is a very constrained special case of inheriting a full-blown class.

And this is where "forced us to add the concept of interfaces" comes in: you cannot do much OO design when constrained to single inheritance only, for example there are serious issues with not being able to reuse code when code reuse is in fact one of the most common arguments for OO. You have to do something more, and the next step is adding multiple inheritance but only for classes that satisfy the constraints of an interface.

So, I interpret Krzysztof's quote as saying

Multiple inheritance in the general case is a very thorny problem that we could not tackle in a satisfactory manner given real-life constraints on the development of .NET. However, interface inheritance is both much simpler to tackle and of supreme importance in OOP, so we did put that in. But of course interfaces also come with their own set of problems, mainly regarding how the BCL is structured.

like image 79
Jon Avatar answered Sep 28 '22 00:09

Jon