5.4. 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
  = [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 unusual, except in the case of initializers, where the designated initializer of the subclass invokes the designated initializer of the superclass. Though actually different these methods are identical conceptually.

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.