10

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?

Mladen Jablanović
  • 43,461
  • 10
  • 90
  • 113
  • Not sure what you're fishing for here: as Bozho says, interfaces are a language-level hedge against someone changing a class' behavior unexpectedly by providing an implementation for methods that used to be abstract. – Jeff Sternal Dec 10 '10 at 16:19
  • The question is whether MI languages really need that hedge or not (see Ken's comments below, for example). I was also hoping to get another opinion from outside of Java/C# camps. – Mladen Jablanović Dec 10 '10 at 16:32

7 Answers7

8

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.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    I don't get it. If it's documented that it is and should remain an interface, why would one be inclined to add an implementation? If the claim is that it's possible to make something semantically correct today, and then somebody might later modify it in a way that makes a big mess, isn't that true of every language feature? – Ken Dec 02 '10 at 16:13
  • Does it then mean that, for example, C++ suffers from not having interfaces? – Mladen Jablanović Dec 02 '10 at 16:29
  • 2
    about C++ - it's subjective. I don't have experience with it to tell whether it suffers. @Ken - the harder it is to misuse something, the better. – Bozho Dec 02 '10 at 16:35
  • 3
    Does anyone, when using a language with MI, have trouble with other people adding implementation to their interfaces? I've never even *heard* of this happening. This same argument could be used against immutable classes (someone could be inclined to add mutation methods!) but I've never seen that happen, either, and immutable classes (in systems that don't have that as a language feature, which is most of them) are tremendously beneficial. – Ken Dec 02 '10 at 16:57
  • @Ken Obviously this is a completely made-up problem, an excuse of the lack of MI. Like the fantasy where programmers abuse operator overloading so that `operator/` means addition and `operator^` means minimize the window. About as credible as a Simpson Halloween episode. – curiousguy Nov 01 '11 at 02:59
  • @Bozho I am at the learning stage of C#, Java theory (although I have coded blindly not giving much importance to it - then I realize theory is at utmost importance since I can't grow further without it). So could you please help me understand what do you mean by *"Interfaces define contracts without specifying implementations?"* with an example? – aspiring Feb 15 '13 at 09:31
8

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
  • The two tasks are not equivalent, but they are undoubtedly related. Inheriting a class implies knowing how to converse, doesn't it? – Mladen Jablanović Dec 10 '10 at 18:25
  • Yes — the greater includes the lesser. But I think that the ability to distinguish between where an author expects you to have a dialogue and where the author expects you to be gifted functionality is sufficiently much of a benefit in a complicated system for interfaces not to be redundant. – Tommy Dec 10 '10 at 19:36
7

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

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..."

TrueWill
  • 25,132
  • 10
  • 101
  • 150
5

There are languages that support multiple inheritance that do not include a parallel concept to the Java interface. Eiffel is one of them. Bertrand Meyer did not see the need for them, since there was the ability to define a deferred class (which is something most folks call an abstract class) with a fleshed out contract.

The lack of multiple inheritance can lead to situations where a programmer needs to create a utility class or the like to prevent writing duplicated code in objects that implement the same interface.

It may be that the presence of the contract was a significant contribution to the absence of a completely implementation free concept of an interface.... Contracts are harder to write without some implementation details to test against.

So, technically interfaces are redundant in a language that supports MI.

But, as others have pointed out... multiple inheritance can be a very tricky thing to use correctly, all the time. I know I couldn't... and I worked for Meyer as he was drafting Object Oriented Software Construction, 2nd edition.

vkraemer
  • 9,864
  • 2
  • 30
  • 44
  • 1
    _"There are languages that support multiple inheritance that do not include a parallel concept to the Java interface."_ Do you know any example for the opposite - i.e. MI language which _has_ separate interface concept? – Mladen Jablanović Dec 16 '10 at 11:01
4

Are interfaces in C# and Java just a consequence of the fact that they do not support multiple inheritance?

Yes, they are. At least in Java. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple). Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. See there for details

And, yes, I believe that like in C++ Interfaces are redundant, if you have multiple inheritance. If you have a more powerful feature, why to keep the less one?

Draco Ater
  • 20,820
  • 8
  • 62
  • 86
2

Well, if you go this way, you could say that C and C++, C# and oll other high level languages are redundant because you can code anything you want using assembly. Sure you don't absolutely need these high level languages, however, they help ... a lot.

All these languages come with various utilities. For some of them, the interface concept is one of these utilities. So yes, in C++, you could avoid using interfaces an stick with abstract classes without implementation.

As a matter of fact, if you want to program Microsoft COM with C, although C doesn't know the interface concept, you can do it because all .h files define interfaces this way:

#if defined(__cplusplus) && !defined(CINTERFACE)
    MIDL_INTERFACE("ABCDE000-0000-0000-0000-000000000000")
    IMyInterface : public IUnknown
    {
   ...
    }
#else   /* C style interface */
    typedef struct IMyInterfaceVtbl
    {
        BEGIN_INTERFACE

        HRESULT ( STDMETHODCALLTYPE *SomMethod )(... ...);

        END_INTERFACE
    } IMyInterfaceVtbl;

    interface IMyInterface
    {
        CONST_VTBL struct IMyInterfaceVtbl *lpVtbl;
    };
#endif

Some kind of another syntactic sugar...

And it's true to say that in C#, if I hadn't the interface concept, I don't know how I could really code :). In C#, we absolutely need interfaces.

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • But C# doesn't have MI, right? Note that I am not at all questioning the benefit of interfaces. – Mladen Jablanović Dec 10 '10 at 18:20
  • absolutely, C# does not have MI. The whole .NET CLR does not have it, but some languages "emulate" it, like Eiffel: http://msdn.microsoft.com/en-us/library/ms973898.aspx – Simon Mourier Dec 11 '10 at 03:37
1

Interfaces are preferable to multiple inheritance since inheritance violates encapsulation according to "Effective Java" Item 16, Favor composition over inheritance.

jacknad
  • 13,483
  • 40
  • 124
  • 194