Rexx Classes for Organizing Objects

Rexx provides several key classes that form the basis for building class hierarchies.

The Object Class

Because the topmost class in the hierarchy, or the root class, is the Object class, everything below it is an object. To interact with each other, objects require their own actions, called methods. These methods, which encode actions that are needed by all objects, belong to the Object class.

Every other class in the hierarchy inherits the methods of the root class. Inheritance is the handing down of methods from a "parent" class--called a superclass--to all of its "descendent" classes--called subclasses. Finally, instances acquire methods from their classes. Any method created for the Object class is automatically made available to every other class in the hierarchy.

The Class Class

The Class class is used for generating new classes. If a class is like a factory for producing instances, Class is like a factory for producing factories. Class is the parent of every new class in the hierarchy, and these all inherit Class-like characteristics. Class-like characteristics are methods and related variables, which reside in Class, to be used by all classes.

A class that can be used to create another class is called a metaclass. The Class class is unique among Rexx classes in that it is the only metaclass that Rexx provides (see Metaclasses). As such, the Class's methods not only make new classes, they make methods for use by the new class and its instances. They also make methods that only the new class itself can use, but not its instances. These are called class methods. They give a new class some power that is denied to its instances.

Because each instance of Class is another class, that class inherits the Class's instance methods as class methods. Thus if Class generates a Pizza factory instance, the factory-running actions (Class's instance methods) become the class methods of the Pizza factory. Factory operations are class methods, and any new methods created to manipulate pizzas would be instance methods:

Figure 5-1. How Subclasses Inherit Instance Methods from the Class Class

As a programmer, you typically create classes by using directives, rather than the methods of the Class class. In particular, you'll use the ::CLASS directive, described later in this section. The ::CLASS directive is a kind of Rexx clause that allows class definitions to be saved permanently, in a file, where they can be reused by other programs. Creating classes by using Class methods sent as messages is not recommended if permanency or reuse is required. At any rate, directives have class-creating powers similar to the Class methods.