Re: Question on abstract classes versus interfaces
- From: "Chris Uppal" <chris.uppal@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 25 May 2006 13:32:44 +0100
alexandre_paterson@xxxxxxxx wrote:
- if "MI of interfaces" isn't like MI at all, like you're saying,
do you conside single inheritance of interface inheritance at all?
I hadn't thought of it before but now you mention it, no I don't. There is
(single) inheritance between Java classes, signified by the keyword 'extends'.
And there is (multiple) inheritance between Java interfaces, also signified by
the keyword 'extends'. But I don't think the relationship between a class and
the interfaces it implements should be called inheritance at all. And,
indeed, it is signified by a different keyword in Java -- so a hearty "Well
done!" to Mr Gosling ;-)
- do you think it is mandatory to rely on concrete inheritance
to achieve OOP ?
Well, it certainly isn't /mandatory/. OO is about objects with polymorphic
behaviour; inheritance (although ubiquitous) is not at all central to OO --
it's hardly even relevant to OO. This is where the notion of interface ==
contract may help. Consider this:
feed(Object animal) // <-- note: "Object"
{
animal.eat();
}
In many languages this would work perfectly well, provided that the objects
passed to feed() did /in fact/ have methods called eat(). So we would have
polymorphic behaviour among all objects which have an eat() method. But what if
one /didn't/ have such a method ? Languages have many ways of dealing with
this; the Java way is for the compiler to try to prove, in advance, that it
can't happen. One way to do that is to set up a promise by the object that it
will have a method called eat():
class Cow
implements FarmInhabitant
{
void eat{) { .... }
}
and a requirement by the user of the object that it must have the method:
feed(FarmInhabitant animal)
{
animal.eat();
}
Now we have a requirement and a promise to satsify it. The compiler checks
that the contract is satisfied, which it is, and so allows the code. There's
no inheritance involved at all.
(There are other ways to express this using words like "type", instead of
"contract", and they are perfectly correct too. But I believe that they tend
to divert attention away from the pure OO concept of polymorphic behaviour. In
particular they tend to produce a confusion between inheritance and
polymorphism.)
It would be perfectly possible to define a completely OO language which didn't
have inheritance (concrete or otherwise) at all. Such a language would have
either a dynamic type system, or a fiendishly complicated static one, but it
could be built, and it would work, and one could do clean OO programming in it.
It would be kind of hard to work with, though. Inheritance is used for two
very important things in real life languages: labour saving and expressing
/necessary/ similarities. My hypothetical language would be terrible at both
(unless it used some innovative scheme for code-sharing without
inheritance[*]).
Switching to the more relevant question of whether one can do OO without
concrete inheritance /in Java/, I would say that roughly the same answer comes
out. It is certainly /possible/ to do clean OO using only interfaces to manage
polymorphism, but in practice it's a right pain in the arse. So, although one
can achieve that level of cleanliness, it's probably better to save it for
"important" places, such as package boundaries, or boundaries between
independent APIs.
-- chris
([*] Delegation-based OO languages, such as Self and ECMAScript, fit this
description.)
.
- Follow-Ups:
- Re: Question on abstract classes versus interfaces
- From: alexandre_paterson
- Re: Question on abstract classes versus interfaces
- References:
- Question on abstract classes versus interfaces
- From: mdinino
- Re: Question on abstract classes versus interfaces
- From: Chris Uppal
- Re: Question on abstract classes versus interfaces
- From: alexandre_paterson
- Question on abstract classes versus interfaces
- Prev by Date: log4j, threading and appenders
- Next by Date: JDK 1.5 support for AIX
- Previous by thread: Re: Question on abstract classes versus interfaces
- Next by thread: Re: Question on abstract classes versus interfaces
- Index(es):
Relevant Pages
|
|