[Tutor] classes and inheritance
Magnus Lycka
magnus@thinkware.se
Sun, 29 Sep 2002 04:35:21 +0200
At 19:23 2002-09-28 -0700, "CameronStoner" wrote:
>Hi all,
>
>Say I have a class Animal , Mamal, and Human. Then go on to say=20
>that Mamal inherits from Animal and that Human does from Mamal. Can you=
=20
>make an object from Mamal that then is a generic Mamal that doesn't have=20
>the attributes of Human and is this a good programming practice?
Well... Yes and no...
It's really pointless to dicuss classes without knowing what
kind of problem you are trying to solve. Classes are not written
to model the world, but to serve some kind of function.
I think you will be better of if you think of a concrete program,
and the classes evolve because they are needed in the
program, not being written as an empty exercise.
Anyway, all animals are of a species. You will never see a
"generic animal" walking in the forest. So, if your classes
are to correspond to the real world, you will never have any
instances of the animal class. It will be an abstract base class.
Although, this might not be true... Your program might be
working with generalizations in a concrete way I suppose...
There are certainly base classes that aren't abstract though.
But not if the base class represents some kind of generalizations
that don't have an individual real work existence.
You can imagine a person class that you use
as it is in an address book program. A person
has a name, and one or several addresses,
phone numbers, email addresses etc. You reuse
this class in a HR application, but there you
need some extra features, so you subclass an
employee class which keeps track of salary,
vacation status, manager, pension fund payments
etc. And in the sales contact program you make
another subclass from person called sucker, no
I mean customer of course, keeping tracks of
orders, complaints and preferences etc. In this
case, person is a concrete base class.
--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se