Using SUPER for invoking functionality in a base class

I Love Xbase++ (ILX)
The portal for Xbase++ developers worldwide

Till Warweg

Member
Staff member
I am here to help you!
Feb 1, 2021
38
11
8
Customer Identifier
E074775
One of the lesser-known features in the OOP engine of Xbase++ is the concept of SUPER, which can be used for invoking functionality implemented in a base class. SUPER exists in two flavors:
  • The SUPER "pseudo-variable" which like SELF, can be used for addressing a certain level in the class hierarchy. While SELF is synonymous for the current class/hierarchy level, SUPER instead references one of the base classes.
    Example:
    Xbase++:
    SELF:doSkip()   // calls :doSkip() in the current class
    SUPER:doSkip()  // calls :doSkip() in a base class
  • The SUPER statement which delegates the current method call to a base class implementation.
    Example:
    Xbase++:
    METHOD myClass:skip( nRecs )
       nRecs += 12
       SUPER  // Calls :skip( nRecs ) in a base class
    RETURN
Both instances of SUPER select and call a method in a base class without having to explicitly state the base class' name. This protects the code from changes in the class hierarchy, for example, the renaming of a base class. Without using SUPER, all method calls involving the old class name would have to be identified and corrected. With SUPER, no code changes are required in this case.
SUPER makes the code more robust by protecting it against changes in the class hierarchy.

Whereas the SUPER "pseudo-variable" routes the call to a method which must be stated explicitly (as in SUPER:doSkip()), the SUPER statement implictly uses the current method's name and parameter profile for identifying and calling the desired method. This makes the SUPER statement ideal for invoking an inherited implementation of the same method when specializing an object's behavior.

The SUA runtime PER statement makes the code more robust against changes in the method's parameter profile. The same method name and parameter profile is used in the call. Parameters that were passed by reference to the current method are also passed by reference to the base class implementation.

For the reasons outlined above, using SUPER is the preferred way of invoking functionality in a base class and should be used in favor of hard-coded "casts" such as ::myBase:doSkip().

SUPER begins searching for the desired method in the immediate base class. If no such method exists, SUPER walks up the the class hierarchy and repeats the process. A runtime error occurs if SUPER is used for calling a method which isn't implemented/available in any base class.