OO COBOL: Class Definitions

In IBM's implementation of OO COBOL, a class definition has four divisions, like an ordinary program, but with special features.

IDENTIFICATION DIVISION

Instead of a PROGRAM-ID, a class definition has a CLASS-ID, followed by the name of the class:
 CLASS-ID. Foobar INHERITS SOMObject.
The INHERITS clause specifies the base class. All classes inherit directly or indirectly from SOMObject, a built-in generic class.

The INHERITS clause may specify multiple inheritance by listing multiple base classes. The order in which the base classes are listed is significant. When two base classes have methods with the same name, the derived class inherits the method from whichever base class is listed earlier.

ENVIRONMENT DIVISION

In the CONFIGURATION section, a special REPOSITORY paragraph must declare each of the base classes and any other classes used by the methods. Optionally, it may also declare the class being defined:
 REPOSITORY.  CLASS SOMObject IS 'SOMObject'
              CLASS Foobar    IS 'Foobar'
              CLASS Barfoo    IS 'Barfoo'.
Each CLASS clause maps the name of a class to the name by which it is known in the Interface Repository (IR). Thus a CLASS clause does for objects what SELECT...ASSIGN does for files.

In my examples, the internal names match the external names. However, there is probably nothing to stop you from coding CLASS GezorkaPlatz IS 'Foobar'.

A class definition cannot have an INPUT-OUTPUT SECTION, because it cannot allocate any files. Methods, however, may access files.

DATA DIVISION

The DATA DIVISION, if present, can contain only a WORKING-STORAGE section, which defines per-instance data -- each instance has its own set of WORKING-STORAGE variables. The syntax is similar to that of an ordinary WORKING-STORAGE section, except: The items in WORKING-STORAGE are accessible only to the methods of the class being defined.

PROCEDURE DIVISION

Here's where things get weird.

The PROCEDURE DIVISION consists entirely of method definitions, one after another. Each method definition may have four divisions of its own, including a PROCEDURE DIVISION which performs the actions of the method.

As a result, the entire class definition may contain multiple WORKING-STORAGE sections, multiple PROCEDURE divisions, and so forth. It takes getting used to, especially when you're trying to find your way around in the editor.

Termination

The last statement in a class definition is a terminator:
 END CLASS Foobar.

[home]Cobol Home [OOC]OO Cobol