next up previous contents
Next: Object initialization Up: Objects Previous: Method overriding   Contents

Messaging super

In the previous section, the nextValue method in the TwoCounter class was a complete rewrite of the original method. If the method being overridden performs quite a heavy task, it is a waste to have to fully rewrite or copy it, when all that is needed is a slight modification before or afterwards. In the example, all that is needed by the nextValue of TwoCounter is an increase of the current_value before the code of the original method.

The original method can still be invoked, by messaging the special receiver super, as is show in this example:

redefine int
  nextValue
{
  current_value++;
  = [super nextValue];
}

The effect of messaging super is that the method invoked will be the method defined or inherited by the superclass of the current class. The value of self within that method will be the same as in the current method, thus messaging super is like messaging self, with the only difference being an indication which class is to provide the method implementation.

Messaging super need not just concern the method overridden by the current method. Any method can be invoked, though that is highly unusual4.2.

The redefine qualifier shown in the example method definitions in this section are actually optional. The compiler can be directed to issue a warning when a redefine is omitted, but due to reasons to become clear later, the presence of redefine can not be required.


next up previous contents
Next: Object initialization Up: Objects Previous: Method overriding   Contents
Pieter J. Schoenmakers tiggr at gerbil.org