Next: , Previous: , Up: Methods   [Index]


Self and super

self and super

The keywords self and super refer to the method’s receiver object and a method in a superclass of the receiver’s class, respectively.

Refer to the method + (class Integer) from the previous section. If you call + with the statement below, self in the + method would refer to i.

j = i + 2;

The super keyword refers to a method in one of the superclasses of the receiver. It is commonly used in constructors.

For example, if a class implements its own new method that performs additional initialization for instances of a class, it can also call new implemented by a superclass.

This constructor from the ReadFileStream method, new, also calls new from class Object.

ReadFileStream instanceMethod new (char *name) {

  ReadFileStream super new name;

  __ctalkInstanceVarsFromClassObject (name);

  ReadFileStream classInit;
  
  return name;
}

The first statement, ReadFileStream super new name, calls new from class Object before performing its own initialization.