Go to the first, previous, next, last section, table of contents.


Messages to super

A method invocation is a different name for sending a message. Such a message has a single receiver.

receiver:
          expression
        | `super'
        | `super' `(' class_name `)'
        ;

Normally, the receiver is an expression. This expression must resolve to an object type to denote the actual receiver of the message.

When a subclass redefines a method inherited from a superclass, it is often desirable to invoke the overridden method. A good and frequent example of this is provided by init methods: suppose a class Foo has as designated initializer initWith int a, and its subclass Bar has as designated initializer initWith int a and int b. Then the implementation of the latter could look like this:

<doc> The {Bar} designated initializer.  </doc>
id
  initWith int a
       and int b
{
  my_b = b;

  = [super initWith a];
}

In this example, the value returned is what is returned by the invocation of the overridden method. It is of course also possible that the overridden method is called as the first expression, with the extra work performed after that; it all depends on the needs of the method. Example:

<doc> Another example of the {Bar} designated initializer.  </doc>
id
  initWith int a
       and int b
{
  if (![super initWith a])
    return nil;

  /* Only foobar {b} if we're alive and
     super's initialization didn't fail.  */
  my_b = foobar (b);

  = self;
}

It is possible for an invocation of a method to super to be unresolvable because differing implementations of the method are provided by the superclasses of the current class. In this case, the last alternative of the receiver syntax should be used to direct the attention of the compiler in the direction of the right superclass. For example, if a class A is a direct subclass of both B and C, and these classes respond to foo differently, the invocation of [super foo] from A will be unresolvable. By replacing this expression by [super (A) foo] or [super (B) foo], the expression becomes resolvable since the compiler is told which method to prefer.

Note the following details concerning sending messages to super:


Go to the first, previous, next, last section, table of contents.