|
Primer : Superclass (Part 1) |
|
|
The Smalltalk language has only 5 reserved words: super happens to be one
of those five. It concerns the hierarchy within the object-oriented world.
|
|
This primer explains the term super by giving some background information
on its role in OO programming, and then by examining a line of code where super
is typically used.
|
|
What does the term "super" mean?The term super is short for superclass and in it's simplest form, it means "the class above the one you're at". For example, take the heirarchy below:AnimalMammalDogGerman ShepherdThe class of Dog would be the superclass of the German Shepherd class. The class of Animal would be the superclass of the Mammal class. The Mammal class would would be the superclass of the Dog class. You get the picture. While we're on the subject, we might as well introduce you to another related term - subclass. Using the example above, the class of Dog would be a subclass of the Mammal class. The class of German Shepherd would be a subclass of the Dog class. The reason for structuring things this way is quite simple - it's how the real world is structured. And borrowing from the real world, this heirarchy has an implicit property called inheritance, another principle behind the object-oriented world. So what does this mean in terms of a computer language? Let's use the following code as an example:
4 squared
In case this statement has not been explained to you before, here it is.
Four (4) is a number. More specifically, it is a particular type of number called an Integer (integers are whole numbers). In VisualWorks, integers are divided into SmallIntegers and LargeIntegers. So, in OO terms, four (4) is an object and SmallInteger is its class. Another way of saying that would be "4 is an instance of the SmallInteger class". It's like re-wording "A beagle is a type of dog" to "Snoppy is an instance of the Dog class".In terms of a hierarchy, it looks something like this. NumberIntegerSmallIntegerHere is the big money question : Where do we find the squared method in the Smalltalk class library? In class SmallInteger? In class Integer? In class Number? Let's suppose that the squared method belongs to the SmallInteger class. That's where Smalltalk will find the method. This would mean that you could only send the squared message to small integers. Since "squared" means any number times itself, it's dumb to limit that function to just small integers. So move it up to the next class, Integer. This would mean that you could only send the squared message to whole numbers, not fractions or decimal numbers. That's dumb too. So move it up to the next class, Number. Now you can send the squared message to all numbers. Now, here's the answer we're looking for. The SmallInteger class inherits all methods of the class above it (Integer) and the Integer class inherits the methods of the class above it (Number). In this sense, when you create a class that is a subclass of another class, you get to use all the methods of that other class and its superclasses. Let's take a code example and see how this works.
^super new initialize
First, the caret (^). Remember, methods in Smalltalk are
similar to subroutines or functions in other languages. The COBOL language calls them
"procedures" and most BASIC dialects call them "SUBs". However, the term "function"
is commonly used when a routine will return or pass back some value. In Smalltalk, when
you want to "return" something, you use the caret (^) sign.
Whenever you have an expression that uses the return caret, remember to always evaluate the entire expression first. The actual "return" is the very last thing that happens. So, moving left to right, Smalltalk will do the following:
^super new
initialize
This would translate as follows. "Whatever class I am, go to the class above me and
perform the new method". Because of inheritance, if the class above didn't
have a new method, it would then go to the class above that one and look for
a new method. If the class above that one didn't have a new method, it
would then go to the class above that one and look for a new method. This search
"up the class hierarchy" would continue until it either found a new method, or
it reached the top of the list without finding one (in which case it would have
returned a message saying "Message Not Understood").
In our specific example, the new method creates an instance of your current class. Wasn't that nice? You didn't have to write that code. So now you have:
(an instance of your class) initialize
So with super new, Smalltalk created an object of our class. We are now sending
our object the initialize message. Smalltalk will look first for an instance-side
method called initialize, and if it is not found, it will then then look in the
superclass.
|
|
SummaryThe expression super new initialize is one of those methods that you always see for every user-defined class. Every object needs a new method, but why write one when one has already been written for you? |