Question about inheritance

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about inheritance

dennis petrocelli
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.
 
 
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.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Question about inheritance

Lukas Renggli
> Any points to clarify my confusion would be greatly appreciated.

This is an interesting question that is often asked at exams ;-)

The first thing to notice is that 'super' references the receiver but
changes the way the method it looked up, therefor

   self = super

is always true. Furthermore

   self class = super class

is true as well, since self and super both represent the receiver,
again super just changes the place where the lookup of #class is
started. "self class" and "super class" both answer the class of the
receiver.

> It seems to me that "super new" and "superclass new" should do the same thing.

Now to come to your example: "super new" starts the lookup of #new in
the superclass, however the receiver of the message is unchanged and
you get an instance of the receiving class. "superclass new" creates
an instance of the superclass, as you would expect the receiver of the
message #new is the superclass.

Cheers,
Lukas

--
Lukas Renggli
http://www.lukas-renggli.ch
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Question about inheritance

Mathieu SUEN
In reply to this post by dennis petrocelli
dennis petrocelli a écrit :

> 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.

This is beceause (super = self) it's only in term of lookup that super is different.
super and self represent the receiver. so 'super new' call new from Object but with the knowlege
that he have to build a Monster object.

#superclass is an method and weusally writ it with a '#' in front of it. so you can't compare super
and a method (super is an object, super = self).
So #superclass give you the object supercass of your Monster (ie Object)

>  
> Thanks,
> Dennis Petrocelli
>
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Question about inheritance

stephane ducasse-2
In reply to this post by Lukas Renggli

On 13 oct. 06, at 16:22, Lukas Renggli wrote:

>> Any points to clarify my confusion would be greatly appreciated.
>
> This is an interesting question that is often asked at exams ;-)

:-)
Dennis who is your prof :)

Stef
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

RE: Question about inheritance

Ron Teitelbaum
In reply to this post by dennis petrocelli
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
[hidden email]


________________________________________
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.

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Question about inheritance

stephane ducasse-2
In reply to this post by dennis petrocelli
Hi dennis

two points:

- aClass new returns an instance of that class

- super represents the receiver of the message but the lookup of the  
method in the superclass
of the class that contains the super call. Once the method is found  
it is applied to the original receiver.

So now
Monster class>> new
       
        ^ super new initialize


is executed when you do Monster new

what is the receiver of the message new => Monster.

So super new is looked up in the super class of Monster and once  
found applied on
Monster, then point 1 makes that you get back an instance of the  
receiver of the message new.

Stef









On 13 oct. 06, at 15:57, dennis petrocelli wrote:

> 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.
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners