Navigation:  Tutorials > Beginners Guide to Smalltalk > Programming in Smalltalk >

Self and Super

Previous pageReturn to chapter overviewNext page

In the last method we added we managed to slip in a couple of new and very important concepts; of those of self and super.

If, in a method, we want to send another message to the receiving object then we use the special word self to represent the object. Take a look at the #printOn: method again to see how this works. We want to use the ability of #displayOn: to print-out the animal's name as a part of the overall representation. We do this by sending the #displayOn: message to self.

Perhaps a more difficult concept to explain is that of super. We use this when we want to send a message to the receiver but we don't want any local declaration (in the receiver's class) of the corresponding method to be executed. Therefore, when you send a message to super, the system will begin its search for the method to execute, not in the class of the receiver, but in its superclass. This is very commonly used when overriding a method. Often you will want to inherit the existing functionality from the overridden method and yet augment it with additional code of your own. This is exactly what is happening in the #printOn: method we have just written.

Question: What would have happened in this method if we had inadvertently sent #printOn: to self rather than super?

It's a sure bet that you'll find self and super cropping up extensively in the code that you write from now on.