When a message is sent to an object, REXX looks for a method whose name exactly matches the message string. 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 by going first to the object specified in the message. If the sought method is not found there, the search continues up the hierarchy. In order, REXX searches for:
An object acquires the methods of its parent class; that is, the class for which the object was created. If the class subsequently receives new methods, objects predating the new methods do not acquire them.
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 doesn't find a match for the message name, REXX checks the object for method name UNKNOWN. If it exists, REXX calls the UNKNOWN method, and returns as the message result any result the UNKNOWN method returns. The UNKNOWN method arguments are the original message name and a REXX array containing the original message arguments (see Defining an UNKNOWN Method). If the object doesn't 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 higher can
be supplemented or overridden by methods existing lower. Searchingthehierarchyforamethod
│
│ │
│ │
superclass
┌──────┘ │
│ ┌─────┴─────┐
│ │ │
superclass superclass
┌─────────────────────┘ │ │
│ ┌──────────┬───────────┤ ├──────────┬──────────┐
│ │ │ │ │ │ │
class class class class class class
│
│ instance instance instance instance instance instance
│ instance instance instance instance instance instance
instance instance instance instance instance instance
│
└─message
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 will be found first. REXX will look no further up the class hierarchy. The instance-level version will override the version at the class level. This technique of overriding lets you change a method used by one instance without disturbing the common method used by all the other instances. It is very powerful for that reason.