Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are interfaces redundant with multiple inheritance?

This is not yet another question about the difference between abstract classes and interfaces, so please think twice before voting to close it.

I am aware that interfaces are essential in those OOP languages which don't support multiple inheritance - such as C# and Java. But what about those with multiple inheritance? Would be a concept of interface (as a specific language feature) redundant in a language with multiple inheritance? I guess that OOP "contract" between classes can be established using abstract classes.

Or, to put it a bit more explicitly, are interfaces in C# and Java just a consequence of the fact that they do not support multiple inheritance?

like image 582
Mladen Jablanović Avatar asked Dec 02 '10 15:12

Mladen Jablanović


3 Answers

Not at all. Interfaces define contracts without specifying implementations.

So they are needed even if multiple inheritance is present - inheritance is about implementation.

Technically, you can use an abstract class in multiple inheritance to simulate an interface. But thus one can be inclined to write some implementation there, which will creates big messes.

like image 79
Bozho Avatar answered Oct 17 '22 03:10

Bozho


Depends on the test for redundancy.

If the test is "can this task be achieved without the language feature" then classes themselves are redundant because there are Turing compete languages without classes. Or, from an engineering base, anything beyond machine code is redundant.

Realistically, the test is a more subtle combination of syntax and semantics. A thing is redundant if it doesn't improve either the syntax or the semantics of a language, for a reasonable number of uses.

In languages that make the distinction, supporting an interface declares that a class knows how to converse in a certain manner. Inheriting from another class imports (and, probably, extends or modifies) the functionality of another class.

Since the two tasks are not logically equivalent, I maintain that interfaces are not redundant. Distinguishing between the two improves the semantics for a large number of programs because it can more specifically indicate programmer intent.

like image 37
Tommy Avatar answered Oct 17 '22 01:10

Tommy


... The lack of multiple inheritance forced us to add the concept of interfaces...

  • Krzysztof Cwalina, in The C# Programming Language (4th Ed.) (p. 56)

So yes, I believe interfaces are redundant given multiple inheritance. You could use pure abstract base classes in a language supporting multiple inheritance or mix-ins.

That said, I'm quite happy with single inheritance most of the time. Eric Lippert makes the point earlier in the same volume (p. 10) that the choice of single inheritance "... eliminates in one stroke many of the complicated corner cases..."

like image 7
TrueWill Avatar answered Oct 17 '22 02:10

TrueWill