Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!cs.utexas.edu!news.ti.com!ticipa!clw
From: clw@ticipa.pac.sc.ti.com (Chris Winemiller)
Subject: Re: How does the instance's class get set?
Message-ID: <1995Jan14.191650.1285@ticipa.pac.sc.ti.com>
Organization: None
References: <guzdial-1401951132340001@guzdial.cc.gatech.edu>
Date: Sat, 14 Jan 1995 19:16:50 GMT
Lines: 71

In article <guzdial-1401951132340001@guzdial.cc.gatech.edu>
guzdial@cc.gatech.edu (Mark Guzdial) writes:

>The idiom for the new class method for a class, say, Apple whose
>superclass is Ball, is:
>
>new
>   ^super new initialize
>
>where Apple has an instance method initialize which may look like this:
>
>initialize
>   size := 10.
>   super initialize.
>
>
>Here are my two questions:
>(1) Why is the instance generated by the "super new" in the "new" class
>method an instance of Apple?  Why isn't an instance of "Ball" (the
>superclass) since it's the superclass that actually did the new?

Because the supereclass didn't "do" the new!  The _receiver_ of the new
message did the new, and the receiver was the Apple class!  Read on..

The special variable "self" always refers to the receiver of a message,
but "super" *also* refers to the receiver of a message. "super" and "self"
refer to the same object, but they differ in where the message lookup
begins for messages sent to them. For self, the message lookup begins in
the class of self. For super, the message lookup begins in the
_superclass_ of self.

In the Apple class>>new method, you have:

    ^super new initialize.

"super" still refers to the Apple class (just like self), but to find
the "new" message sent to super, the message lookup begins in super's
superclass, which is the Ball class. (Note: "super" is *still* a reference
to the Apple class, not the Ball class.) Assuming the Ball class>>new
method returns a new instance of the _receiver_ (which is the Apple
class as referred to by super), then the "super new" expression above
answers an instance of Apple. That instance is then sent the initialize
message, which causes the initialize instance method of Apple to be
executed.

>(2) Why is that "super" in "super new" refers to the Class Ball in the
>first case, but refers to the instance methods of Ball in the second?

In the Apple class>>new method, super and self refer to the receiver of
the "new" method (i.e. the Apple class). When you execute the initialize
instance method of Apple, super and self refer to the receiver, which is
an _instance_ of the Apple class. That method executes two statements:

    size := 10.
    super initialize.

The first statement assigns a value to the "size" instance variable of
the Apple object.  The second statement executes an "initialize"
method for the Apple object, but *begins the method lookup in the
superclass* (which is Ball).  So, the instance of Apple executes the
definition of initialize found in Ball.  Executing a superclass
implementation of a method doesn't change the nature of "self".  self is
still an Apple. And super is still that same Apple.

Regards,
Chris
==============================================================
Chris Winemiller               Internet: clw@works.ti.com
Disclaimer: I do not speak for TI.
==============================================================

