[Tutor] designing POOP

Alan Gauld alan.gauld at btinternet.com
Wed Feb 6 09:58:09 CET 2008


"Tiago Saboga" <tiagosaboga at terra.com.br> wrote

>> sub types of an abstract superclass. Do NOT use data
>> attributes for this, always base inheritance heirarchies
>> on behaviour.
>
> Could you please elaborate on this last sentence? I don't understand
> what you mean, and I feel I may be on this track.

Inheritance heirarchies are the basis of polymorphism in OOP.
Polymorphism is all about methods not data. Therefore to create
useful inheritance heirarchies the key is to find groups of objects
that share the same behaviour (ie method set) even if the internal
data (the attributres) are different.

The classic example of polymorphism is shapes.
Now if you look at the data attributres of a set of shapes: square,
circle, triangle etc they look quite different. (radius v lenxbreadth,
height, angles etc) So from a data perspective they are not good
inheritance candidates. But idf you look at their behaviours - draw,
calculate area, move, intersect etc they are all shared, thus we
can create an abstract superclass with those behaviours. Each
subclass can provide its own internal data and implementt the
methods using that data but the external interface of all shapes
is identical and therefore polymorphiasm can be used.

By contrast if you take some objects that have the same data
but different methods and try to create a superclass you will
simply wind up with a very big class that aggregates all the
methods of all the objects but cannot be used in a polymorphic
way because only a subaset of the methods actually apply to
any given instance.

Thus always base inheriotance on common mbehaviour not on
common data.

I hope that makes it clearer.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list