Home Segments Index Top Previous Next

254: Mainline

Inside your definition of new for the Food class, you need to tell Smalltalk to send new to Food, the value of self, to create a new food instance. However, you do not want Smalltalk to use the new method that you are defining, because that would lead to unbounded recursion. Instead, you want Smalltalk to use the new method associated with the direct superclass of the Food class, rather than with the Food class itself.

Fortunately, whenever a message is sent to super in a method definition, Smalltalk acts as though the message were sent to self, except that Smalltalk initiates method search in the direct superclass of the class for which the method is defined.

Thus, you use super in the definition of a new method for the Food class:

Food method definition • class 
 *-- Define the new method for the Food class 
 |  
 |         *-- Send the new message to the value 
 v         |   of super (same as value of self) 
new        v 
  ^ super new 
  ^   ^ 
  |   | 
  |   *-- Look for the new message associated with the 
  |       direct superclass of the Food class 
  | 
  *-- Answer the new instance