Inheritance vs Composition (was Re: C++)

Aahz Maruch aahz at netcom.com
Thu Dec 16 11:24:57 EST 1999


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.

[I'm blanking on where I first saw this crucial distinction; anyone who
wants to remind me would be very welcome.]
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>     http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6

TEOTWAWKI -- 16 days and counting!



More information about the Python-list mailing list