The Default Search Order for Selecting a Method

When a message is sent to an object, Rexx looks for a method whose name matches the message string ignoring case. If the message is ADD, for example, Rexx looks for a method named ADD. Because, in the class hierarchy, there may be more than one method with the same name, Rexx begins its search at the object specified in the message. If the sought method is not found there, the search continues up the hierarchy. Rexx searches in the following order:

  1. A method the object defines itself (with SETMETHOD or ENHANCED).
  2. A method the object's class defines.

    An object acquires the methods of its parent class (that is, the class that created the object). If the class subsequently receives new methods, objects predating the new methods do not acquire them.

  3. A method an object's superclass(es) define.

    As with the object's class, only methods that existed in the superclass when the object was created are valid. Rexx searches the superclass method definitions in the order that INHERIT messages were sent to an object's class.

If Rexx does not find a match for the message name, Rexx checks the object for a method named UNKNOWN. If it exists, Rexx calls the UNKNOWN method, and returns whatever the UNKNOWN method returns. For more information on the UNKNOWN method, see Defining an UNKNOWN Method . If the object does not have an UNKNOWN method, Rexx raises a NOMETHOD condition. Any trapped information can then be inspected using Rexx's CONDITION built-in function.

Rexx searches up the hierarchy so that methods existing in classes at higher levels can be found. This search realizes the inheritance of methods from superclasses.

Searching the Hierarchy for a Method

For example, suppose you wrote a program that allows users to look up other users' phone numbers. Your program includes a class called Phone_Directory, and all its instances are users' names with phone numbers. You have included a method in Phone_Directory called NOTIFY that reports some data to a file whenever someone looks up a number. All instances of Phone_Directory use the NOTIFY method.

Now you decide you want NOTIFY, in addition to its normal handling, to personally inform you whenever anyone looks up your number. To accommodate this special case for your name only, you create your own NOTIFY method that adds the new task and replicates the file-handling task. You save the new method as part of your own name instance, retaining the same name, NOTIFY.

Now, when a NOTIFY message is sent to your name instance, the new version of NOTIFY is found first. Rexx does not look further up the class hierarchy. The instance-level version overrides the version at the class level. This technique of overriding lets you change a method used by one instance ("one-off" instance) without disturbing the common method used by all the other instances. It is very powerful for that reason.