[Newbies] Question about inheritance

Ron Teitelbaum Ron at USMedRec.com
Fri Oct 13 19:41:32 UTC 2006


Hi Dennis,

I don't think I've seen you on this list so welcome!  I think your question
is a very good one.  I read the other responses you received and they are
very good.  I thought I'd give you a practical example of what is called a
factory pattern to explain why this is so important and how it works.

A factory pattern is basically a pattern that decides what class to build
and gives it back to you.  In other words you send it off to the factory
method and it builds an instance of the class that you need.

Ok so now an example: 

Person class>>newForAge: anAge
	"Instantiate and return to the sender the class that is appropriate
for anAge"
	aClass := Person subclasses detect: [:aClass | 
		aClass ageRange includes: anAge] ifNone: [self error: 'Age
Range for: ', anAge asString, ' is not defined in subclasses of person].
	^aClass createWithAge: anAge

Ok so that is the factory.  It says go through my subclasses and find one
that matches anAge then create an instance of that class with that age.

To make this work we need classes and ageRanges so we create subclasses of
Person 

Person subclass: SeniorPerson
Person subclass: AdultPerson
Person subclass: MinorPerson

Now age ranges

SeniorPerson class > ageRange
	^NumericRange from: 65 to: 999

AdultPerson class > ageRange
	^NumericRange from: 21 to: 65

MinorPerson class > ageRange
	^NumericRange from: 0 to 21

(Ok so there is no NumericRange, I looked but didn't find one but you can
build it yourself.  You just need to implement #includes: aNumber in it)

Ok so now here comes the part that answers your question.

Say there is code that you want run on all of these classes, do you
implement the code on each subclass?  Is there some way to write the code on
the superclass so that it can be in just one place?

The answer is yes!  You can put the code on the superclass.

Person createWithAge: anAge
	^self new
		age: anAge;
		yourself

This might look strange but look at what it is doing.  It creates an
instance and sets the age, which since you would define the value age on the
superclass Person and not on each subclass this makes sense.

Now how can we use this method to create a subclass?

Like this: 

Senior class > createWithAge: anAge
	"Return to the sender an instance of Senior with anAge set"
	^super createWithAge: anAge

Now look closely.  What this says is run the method createWithImage: anAge
of the superclass for me (Senior class).  When the method executes self on
person = Senior class.  Try debugging the method and see.

You could use this to further specialize the object.

Senior class > createWithAge: anAge
	"return to the sender an instance of the receiver"
	| aSenior |
	aSenior := super createWithAge: anAge.
	"set Senior specific values"
	aSenior seniorDiscount: (self seniorDiscountForAge: anAge).
	^aSenior

So you can see super really means run a superclass method on the class or
instance that calls it.  The self of the method that is called is the
instance or class that called it.

I hope that helped, if not please say so,

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
Ron at USMedRec.com 


________________________________________
From: dennis petrocelli
Sent: Friday, October 13, 2006 9:57 AM

Greetings smalltalkers,
 
I'm a newbie trying to get my head around inheritance.  I'm puzzled by the
following example.  It seems to do what I mean, but not what I type ;)  This
is a good thing, but I'd get further if I understood the semantics a bit
better.
 
from
http://www.cosc.canterbury.ac.nz/wolfgang.kreutzer/cosc205/smalltalk1.html
 
Class: Monster
Superclass: Object
Category: Sesame Street
Instance variables: colour tummy
 
initialization
 
initialize
   self colour: #green.
self tummy: Bag new
 
"There is only a single class method and no class variables."
 
creation
new
   ^ super new initialize
 
What I don't understand is this:  Monster new returns an instance of
Monster, what you'd want it to. But Monster new executes "^ super new" which
to my mind should return an instance of Object, not Monster.  Just to
confuse myself further, I typed:
 
Monster superclass new 
 
and got an Object.  It seems to me that "super new" and "superclass new"
should do the same thing.
 
Any points to clarify my confusion would be greatly appreciated.
 
Thanks,
Dennis Petrocelli  
  
________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail.



More information about the Beginners mailing list