Interface inheritance vs Implementation inheritance.
(x-posted to c.object and c.l.java.programmer)
First, a quick bit about my background; most of my OO experience comes
from Java, with some C++ in my past. Recently in comp.object, there was
a thread "Inheritance of implementation: what is reason of". While the
subject line could use some grammatic tweaking, the point was eventually
made clear.
I was thinking about it, and it seems to me that implementation
inheritance is a convenience that leads to bad design and mistakes. By
convenience, I mean that often a programmer will design the an interface
and the implementation together in one source unit (e.g. a class).
Alternatively, you can define a separate interface (lets call it IFoo)
in one source unit, and an implementation (FooImpl). FooImpl is *also*
an interface in many languages, it just happens to have the associated
implementation.
So far, not that big of a deal. The convolution comes when you add in
access modifiers. For instance, in Java you can mark methods/fields as
private, protected, package-private, or public. Now, you have up to 4
interfaces to the implementation. Only certain code units can see some
of the interfaces, but managing that contract becomes troublesome,
especially for the "protected" interface, as many programmers don't
design their classes for inheritance but the class gets used that way.
To me, polymorphic behavior is important, and "implementation sharing"
is a very useful convenience. I think that they are often co-mingled
concepts that should be separated. I have a few ideas on how I might do
this, at the language level, but I wanted to see if my above thoughts on
the subject stirred any interesting discussions.
My main point is that classes should use composition to gain behavior,
possibly with an easy mechanism for delegation. The *only* thing that
should be automatically inherited should be the interface. This
eliminates the need for (and usefulness of) the "protected" access modifier.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>