Inheritance vs Composition (was Re: C++)

Boudewijn Rempt boud at rempt.xs4all.nl
Thu Dec 16 12:43:45 EST 1999


Aahz Maruch <aahz at netcom.com> wrote:
> In article <838uvo$sc7$1 at news1.xs4all.nl>,
> Boudewijn Rempt <boud at rempt.xs4all.nl> wrote:
>>
>>That makes C++ about as powerful as Visual Basic - in essence, no
>>inheritance at all, just interfaces... Having just done a large project
>>in Visual Basic, I've learnt how painfult that limitation can be - and
>>I was kind of surprised when I read in Design Patterns that composition
>>should be favoured over inheritance. There are no doubt good reasons,
>>but not one I can think of.

> Let me clarify: the difference between inheritance and composition is
> the difference between "is-a" and "has-a".  For example, you would not
> design a "car" object to be inherited from "engine", "chassis",
> "transmission", and so on ("car is-a engine" makes no sense) -- you
> would *compose* the car object from its constituent parts ("car has-a
> engine" makes a lot of sense).

> Once you start thinking in terms of "is-a" versus "has-a", it's a lot
> easier to do proper design.

Well, that's clear - even to me, and I am the very epitome of corporate
cubicle programmerhood (Sinologist, retrained in six month for Oracle
work, never read a good cs book until I started with Python). And that's
why I hate it when I can't - easily - use VB to build a 'better' grid
control - when I have to build a control that has a grid... It's not
just laziness, but also schedule, cost and maintenaince pressures.
Indeed, when I program for fun, I tend to start with doing a simple 
subclass of all the gui components I'm likely to want to customize,
in order to promote consistent behaviour, look and feel.

That's why I was surprised by recent sentiments about the relative value
of inheritance and composition - I guess that the recent trend is just
another fashion, and that the future will balance it.

-- 

Boudewijn Rempt  | http://denden.conlang.org



More information about the Python-list mailing list