|
|
|
Inheritance
Everything we have done so far has been what is known as 'composition'.
This name should suggest composing an object from smaller components,
not a homework assignment in high school. You likely already use composition
in your programs, but lets define exactly what it is to keep it from
blurring later. It is often referred to as a 'has-a' relation as mentioned
earlier. Object A has-an object B. A house has-a kitchen, a dog has-a
tail, a rebel has-a cause. You might be hard pressed to think of an
object that doesn't have-a something or other in it - everything is
made of something after all. Or is it? What about ideas? For example,
how does an appliance relate to a fridge? How does a monkey wrench relate
to wrenches, or tools? How does James Dean relate to Ronald Reagan?
to Elvis? to Che Guverra? You can see that once you start organizing
ideas and concepts, has-a relations are not going to cut it alone. The
tool needed for the job is inheritance - 'is-a' relationships.
Before going further, let's get used to the two ideas. Has-a relations
are when one object contains another. A car has-an engine. An engine
has-some pistons. Pistons have, uhh, this little black ring thing that
goes around them, I think (sorry, that is the limit of my car knowledge!).
Is-a relations are when one thing is a type of another thing. So a car
is-a vehicle, a vehicle is-a machine, a machine is-a man-made tool with
whirring parts that do stuff... (sorry again!). You really should stop
now and try to think of a few inheritance systems. Obviously you didn't
stop, so here's some detail.
Inheritance is not complicated, but it is hard to visualize. There
is nothing in the 'real' world seems to have exactly the same behavior.
This is because inheritance is more of a mental categorization system.
It's easy to spot a mammal when you talk to one, but there is no such
animal as 'mammal', only types of mammals. It doesn't mean the word
mammal is useless of course, just don't expect the zoo to have a cage
that says 'mammal'. Before we talked about objects as boxes, or containers
of some sort. This works well for composition, where you put objects
inside other objects, but it breaks down somewhat when dealing with
inheritance. We will need to find a slightly different model to 'visualize'
inheritance.
We are ahead of ourselves. Let's just make sure we are clear on the
difference between has-a and is-a relations between objects (at least
the easy ones). Go through the list below and decide if the relation
is inheritance or composition. Try to figure out what is common in all
inheritance examples, and what is common to all composition examples:
- Animal, mammal, dog, poodle
- Computer, motherboard, CPU, circuitry
- Entertainer, singer, pop-star, Elvis
- House, kitchen, sink, tap
- Bookstore, computer section, flash section, actionscript section
- Book, computer book, flash book, actionscript book
- Book, chapter, paragraph, sentence, word
- Continent, country, region, city/town, street, house
- Asia, China, Fujian, Xiamen, ZengCuoAn, 15
- http://www.macromedia.com/support/flash/
- Application, document, scrollbar, thumb
- Window, dialog, commonDialog, printDialog
- Food, dessert, pie, banana-cream
- 01 (204) 435-8301
Let's go over these in (not too painstaking) detail.
- Animal, mammal, dog, poodle - Inheritance. A poodle is-a
dog, a dog is-a mammal, a mammal is-an animal. What properties would
each level have? Try attaching the following properties: hair, eyes,
obnoxious, domesticated.
- Computer, motherboard, CPU, circuitry - Composition. A computer
has a motherboard, a motherboard has a CPU... If a computer has an
on/off switch, does a CPU have an on/off switch? (and don't say 'yes,
5 billion'!). Apply that kind of question to animals and dogs.
- Entertainer, singer, pop-star, Elvis - Inheritance. Elvis
was-a pop-star, a pop-star is-a singer, a singer is-an entertainer.
Are all pop-stars singers? Where would you put actors in that list?
Can you see how Elvis is an instance, the rest are classes?
- House, kitchen, sink, tap - Composition. A kitchen has-a
sink. A sink is clearly not a house (unless you are a cockroach in
a college dorm). You could also, theoretically, cut a kitchen out
of a house, put it on the moon, and it would still be a kitchen. It
is an object inside a house.
- Bookstore, computer section, flash section, actionscript section
- Composition. Has-a, has-a, has-a... Each one is physically contained
in the other. You could imagine exchanging components, by putting
the actionscript section in the kitchen that is now on the moon. A
great benefit of using well designed objects is that you can reuse
them in different places.
- Book, computer book, flash book, actionscript book - Inheritance.
These are all types and subtypes. An actionscript book is-a computer
book. Are there any instances in this sequence? What would be an example
of an instance if you added one to the end?
- Book, chapter, paragraph, sentence, word - Composition. Perhaps
a tricky one, in that components do not always have to be physical.
Of course you can touch a paragraph in any given book, but it still
is composition if we are talking about books in general. Books have
chapters, sentence are composed of words. The same is true for previous
examples of composition, but perhaps it is more obvious here. Kitchens
have sinks, and my kitchen has a sink. In both cases the sink is a
component in the kitchen. Don't lose focus!
- Continent, country, region, city/town, street, house - Composition.
This time it must be abstract, and yet it is composition. Clearly
a street is not a type of country. Clearly concepts can be components
of other concepts. The container 'continent' has countries in it.
These countries in turn contain regions...
- Asia, China, Fujian, Xiamen, ZengCuoAn, 15 - Composition.
This is the same hierarchy as the previous, just 'instanciated' -
Each concept is now filled in with an object, but these objects are
certainly components of each other. The continent called Asia has-a
country called China. The country called China has-a city called Xiamen.
- http://www.macromedia.com/support/flash/ - Composition. The
domain macromedia.com has-a section called support. Support has a
section called Flash. This final section is useful when all else fails,
because it has a copy of the manual. Is the folder structure on your
computer composition or inheritance? Are all tree structures composition?
(no, look up..)
- Application, document, scrollbar, thumb - Composition. Yes,
windows programs are object oriented... documents have scrollbars,
scrollbars have thumbs (thumbs are the little things you drag on a
scrollbar).
- Window, dialog, commonDialog, printDialog - Inheritance.
Yes, windows programs use inheritance too. The print dialog box is
a type of window, so are all dialogs.
- Food, dessert, pie, banana-cream - Inheritance. Pie is food.
Dessert is food. Notice this is a tree structure - you could have
many types of dessert, and many types of pies. All of these structures
have been trees.
- 01 (204) 435-8301 - Composition. This is actually the same
as saying North America>Manitoba>Miami>my old phone number.
The phone system, like the postal system, uses a very structured naming
system to find you.
Inheritance, and OO programming in general, becomes most useful when
you get enough similar classes that they start to somewhat naturally
organize themselves (always good to be hopeful!). This is worth mentioning,
because many Flash programs are quite simple and not all that big, and
have short life spans (nor is the programming even the central focus
of many files). It would be overkill to use multiple levels of inheritance
to link buttons to pages. Writing small programs is a bit like being
the post-master in a small village. You need nothing more than the name
of a person to get your mail delivered. If there is a change - someone
new moves into town, someone changes houses, even a street name change
- you can just keep track of it in your head. You might even laugh at
how seriously big city postmasters take obscure rules, or how they debate
for hours tiny changes in the postal codes. This is all fair enough;
you both get the mail delivered. As long as you recognize that the village
system would not work in the city, there is nothing wrong with being
'quick and dirty' when it meets all the requirements - it's probably
advisable in fact. You don't want to subdivide mammals>dogs down
to atoms, after all. One of the main skill sets in OO programming is
finding the right balance between complexity and completeness (the same
dilemma a writer faces when writing a short chapter about object-oriented
programming in actionscript in fact!).
Local
< < Home >
> Prototypes
|
|