Search code examples
smalltalksuperclass

"class" message sent to superclass in Smalltalk


Let us consider following classes:

Object subclass: Sup [].

Sup subclass: Sub [ print_superclass [ super class printOn: stdout. ] ].

When I try to run print_superclass method on Sub I get

> Sub new print_superclass.
Sub

I expected here to get Sup because the class call was moved back to the superclass of Sub which is Sup. Why does it behave like this?


Solution

  • Because super is a pseudo-variable pointing to the receiver of the message. Super and self point to the same object and have the same identity.

    super == self ---> true
    

    The difference between them is that super tells the message lookup to start searching the next in the method dictionary "above" that containing the method.

    The definition is confusing, but in this case, super only says that the method search for #class does not start in Sub methods, but in Sup methods. However, it does not have effect because #class is defined in a higher level of the hierarchy and its implementation refers to the class of the receiver, that is an instance of Sub