[comp.lang.smalltalk] need explanation of 'super class'

becher@argosy.UUCP (Jonathan D. Becher) (07/17/89)

Okay, I need some help.

Suppose I have two classes, Building and Shack, where Shack is derived from
Building. Both Building and Shack have class methods called size.

	Building class methodsFor: 'example'
	size
		^10

	Shack class methodsFor: 'example'
	size
		^5

Now, from within a instance method of Shack ...

	Shack methodsFor: 'test'
	tester
		Transcript show: self class size.	"prints 5"
		Transcript show: super class size.	"prints 5 also"

Why does the second one print 5 and not 10?

Jon Becher			argosy!becher@decwrl.dec.com
"It's only rock 'n' roll, but I like it ..."

becher@argosy.UUCP (Jonathan D. Becher) (07/18/89)

In article <180@argosy.UUCP>, I write:
> Suppose I have two classes, Building and Shack, where Shack is derived from
> Building. Both Building and Shack have class methods called size.

...

> 	Shack methodsFor: 'test'
> 	tester
> 		Transcript print: self class size.	"prints 5"
> 		Transcript print: super class size.	"prints 5 also"
> 
> Why does the second one print 5 and not 10?

Whoops! I was making (as the Blue book describes it) the classic mistake in
understanding super.  Things work fine if I change the second Transcript
line to:
		Transcript print: self superclass size.

Jon Becher			argosy!becher@decwrl.dec.com
"It takes a big man to admit his mistakes.  I'm not a big man." 

alan@oz.nm.paradyne.com (Alan Lovejoy) (07/20/89)

In article <180@argosy.UUCP> becher@argosy.UUCP (Jonathan D. Becher) writes:
>Suppose I have two classes, Building and Shack, where Shack is derived from
>Building. Both Building and Shack have class methods called size.

>	Building class methodsFor: 'example'
>	size
>		^10

>	Shack class methodsFor: 'example'
>	size
>		^5

>Now, from within a instance method of Shack ...

>	Shack methodsFor: 'test'
>	tester
>		Transcript show: self class size.	"prints 5"
>		Transcript show: super class size.	"prints 5 also"

>Why does the second one print 5 and not 10?

Because 'super class size' is evaluated as follows:
1.  The message 'class' is sent to the Shack instance.  The search for the
corresponding method starts in Shack's superclass--which is Building.
Evenutally, a method named 'class' is found, either in Building or one of
its super classes.  The method is executed and (presumably) returns an object
which is the class of the Shack instance--which is, of course, the class Shack.
2.  The message 'size' is sent to the object that resulted from evaluating
'super class'--which is the class Shack.  The search for the method named
'size' starts in Shack's metaclass, where it is immediately found in the
'example' category.  The method is executed and returns the object 5.

So the effect of writing "super class size" instead of "self class size" is to 
evaluate the object's superclass's instance method named 'class', instead of 
the object's class's method named 'class.'  Since most classes do not implement
an instance method named 'class,' this usually makes no difference.

In order to do what you intended to do--execute the object's superclass's
metaclass's method named 'size,' code the following:

self class superclass size.


____"Congress shall have the power to prohibit speech offensive to Congress"____
Alan Lovejoy; alan@pdn; 813-530-2211; AT&T Paradyne: 8550 Ulmerton, Largo, FL.
Disclaimer: I do not speak for AT&T Paradyne.  They do not speak for me. 
Motto: If nanomachines will be able to reconstruct you, YOU AREN'T DEAD YET.