Are interfaces redundant with multiple inheritance

2019-03-18 20:37发布

问题:

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?

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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?



回答6:

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.



回答7:

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