Re: Interface inheritance vs Implementation inheritance.
Robert Martin wrote:
If you think about that a bit longer, you'll realize that an easy way to
create composition IS inheritance. Or rather, inheritance is an easy
way to create composition.
Every method of the component gets redeclared in the composer. When
those methods are called on the composer, the component's implementation
is invoked.
Now change the words composer to base, and component to derivative and
you'll see the sentence is true.
Perhaps what you are after is a way to make sure that the composer
cannot be cast (explicitly or implicitly) to the component. That's what
private inheritance gives you.
I conclude from this that we'd all be happy if Java had multiple
inheritance and private inheritance.
;-)
I disagree. Inheritance and interfaces a la Java have one important
distinction: interfaces introduce no implicit implementation of methods.
Since Java does not support multiple inheritance implementation
inheritance is too limited for many use cases. C++ supports MI but in a
very disturbed way (although I was quite happy with it when it was
introduced into the language I have learned to really hate it). The
issues of virtual bases and multiple down-cast paths leading to
different implementations of the same function signature are painful. It
is unclear how any implementation of multiple implementation inheritance
could circumvent these issues.
The advantage of the Java interface concept is that a class implementing
an interface (and not extending any other class than Object) will have
to explicitly define how the interface methods are implemented. If it
extends multiple interfaces with shared function signatures the seaming
ambiguity is resolved at the class level itself.
Unfortunately Java lacks a simple way of expressing interface
delegation. I would have loved something like:
interface I1
{
public void f1();
public void f2();
public void f3();
}
class C1 implements I1
{
public void f1() { /*...*/ }
public void f2() { /*...*/ }
public void f3() { /*...*/ }
}
class C2 implements I1, I2
{
private C1 c1a;
private C1 c1b;
public I1() { return c1a != null ? c1a : c1b; } /* NOT SUPPORTED */
public void f2() { /* THIS IS AN OVERRULE IMPLEMENTATION */ }
}
where the constructor-like syntax of the 'public I1() { /*...*/}'
notation implies that all methods declared in I1 and not declared in C2
or I2 are implicitly implemented such that given a C2 reference c2
calling c2.f1() is equal to c2.I1().f2().
This syntax would be ideal for implementing partial delegations of
interfaces with many methods when writing wrapper classes (the JDBC or
Servlet interfaces anyone?).
Would such syntax/semantics exist I would be happy to drop
implementation inheritance in most places.
Kind regards,
Silvio Bierman