Re: Composition vs. Inheritance (Was: colored point redux)

Robbert Haarman <[email protected]> Mon, 26 Feb 2007 17:00:34 +0100
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
Daniel,

Thanks for your clear explanation, but that wasn't really what I meant. 
I guess I didn't ask the right question.

The way I would implement things would be the same as the way you've 
pointed things out: have the sequence interface implement some minimal 
set of common methods, and then implement all the other sequence 
operations using that interface. Now, strings and lists only need to 
implement the minimal interface, and they will get the other 
functionality for free.

This is also the way "inheritance" works in my current language design; 
you inherit functionality by implementing the interface that has been 
used to implement that functionality. There is no notion of methods 
being coupled to classes, and thus no inheritance like that provided by 
Java's 'extends' keyword (which I will call 'extension', for brevity).

Perhaps the strength of extension is really in the other scenario I 
mentioned: the case where, for example, an Employee is also a Person, 
and thus inherits all fields and methods from Person. How would you 
implement that with composition? I've seen people recommend something 
like

class Employee {
	Person person;

	...
}

which would then be used like

anEmployee.person.someMethod(...)

but that feels to me like taking a step back from object-oriented 
programming, throwing away its advantages.

Regards,

Bob

-- 
Life is too short for sentences that begin with "Life is too short for".
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFF4wQifb9wcmD+WN4RAjXWAKDVY8VZWLBP0jwL9UZlDgcL+0M3dQCeLjbN
cLNbn6DHBT/iK76zGp5QzHM=
=spVU
-----END PGP SIGNATURE-----