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

Tom Locke <[email protected]> Mon, 26 Feb 2007 19:16:30 +0000
Newsgroups gmane.comp.lang.lightweight
Message-ID <[email protected]>
> Ok, so we'd like an Employee to look like a Person then?  Employee  
> can dispatch methods off to the Person object.
>
> /******************************************/
> class Employee {
>     Person person;
>     Object someMethod(...) {
>         return person.someMethod(...);
>     }
>     ...
> }
> /******************************************/

The big difference between this technique and inheritance is that if  
'person' calls a method on 'this' (sends a message to self), we don't  
get to give the  a new meaning for employees - once we've delegated,  
'this' becomes the regular person.

e.g. in Employee:

   int calculateWages(int hoursWorked) { return this.hourlyRate() *  
hoursWorked; }

Then:

class Manager extends Employee {
     int hourlyRate() { return super.houryRate() * 3; }
}

If Manager contained an Employee, and delegated the calculateWages to  
that object, you wouldn't get the trippled hourly rate.

Apologies if this has already been covered, I'm only half following  
this thread.

Tom

p.s. disclaimer to those who see the world in back-and-white :-) I'm  
not advocating one approach over the other overall. LOL