Classes and Instances

In REXX, objects are organized into classes. The reason is not unlike why biologists categorize living things in hierarchies--to organize them by their similarities and lines of inheritance. Classes are like templates; they define the methods and variables a group of similar objects have in common. Rather than replicate the same methods and variables over and over for each similar object in your program, classes provide an efficient way to store these common actions and characteristics in one place.

If you were writing a program to manipulate some screen icons, for example, you might create an Icon class. In that Icon class you could include all the icon objects with similar actions and characteristics:
A simple class

┌───────────────────


Icon class

OS/2 system icon instance
shredder icon instance
information icon instance
.
.
.

All the icon objects might use common methods like DRAW or ERASE. They might contain common variables like position or color or size. What makes each icon object different from one another is the data assigned in its variables. Perhaps for the OS/2 system icon, position='20,20', while for shredder it's '20,30' and for information it's '20,40':
Icon class

┌───────────────────


Icon class

OS/2 system icon instance
(position='20,20')

shredder icon instance
(position='20,30')

information icon instance
(position='20,40')

Objects that belong to a class are called instances of that class. As instances of the Icon class, the OS/2 system icon, shredder icon, and information icon acquire the methods and variables of that class. The instance objects behave exactly as if they each had their own separate methods and variables of the same names. All the instances actually store, however, are their own unique properties--the data associated with the variables. Everything else can be retained at the class level.
Instances of the Icon class

┌───────────────────


Icon class
(position=)

OS/2 system icon instance
('20,20')

shredder icon instance
('20,30')

information icon instance
('20,40')

A very useful thing about this arrangement is that, should it become necessary to update or change a particular method, you would only have to change it in one place, at the class level. This single update would then be acquired by every new instance that used the method. In cases where a class has hundreds of instances, the efficiency of this approach is clear.

A class that can create instance objects is called an object class. Our hypothetical Icon class is an object class you can use to create other objects with like properties. In the future you may want to create an application icon or a drives icon.

An object class can produce objects as a cookie cutter produces cookies. It is like a factory for producing objects. In particular, these are a class's instance objects.


[Back: Polymorphism]
[Next: Data Abstraction]