Chapter 17: Object COBOL Language Extensions

This chapter describes the COBOL syntax that has been added to support object orientation. Since all of this syntax is an extension to ANS X3.23 - 1985 that is specific to Micro Focus COBOL, the box around this chapter has been omitted. For a full description of the object orientation facility, see your COBOL system documentation.

17.1 Directives

In addition to the Compiler directives which provide flagging and modify the reserved word list, the following directives may impact either the syntax or the semantics described in this section.

17.2 Class Definition

A Class describes an Object COBOL Class object and its instance objects. It contains nested programs for class methods and instance methods.

General Format

Syntax Rules

  1. The words PROTECTED and RESTRICTED are equivalent.

  2. If the EXTERNAL clause is specified, neither the DATA nor the INHERITS clause may be specified.

  3. If the WITH DATA phrase is specified in the INHERITS clause, the DATA IS PRIVATE clause must not be specified explicitly or implicitly in the source code for class-name-2.

  4. Class-name-2 must not be the same as class-name-1.

  5. Class-name-2 must not inherit from class-name-1 directly or indirectly.

  6. Class-name-1 in the END CLASS header must be identical to the class-name-1 specified in the preceding Class-ID paragraph.

General Rules

  1. Class-name-1 identifies the class being declared.

  2. The ABSTRACT clause indicates that class-name-1 is an abstract class. You cannot create an instance of an abstract class.

  3. The EXTERNAL phrase indicates that class-name-1 is an external class. No code is produced.

  4. The RESTRICTED phrase enables subclasses to directly access data inherited from class-name-1.

  5. The PRIVATE phrase prevents subclasses from directly accessing data inherited from class-name-1.

  6. The INHERITS phrase specifies that class-name-2 is a parent class of class-name-1.

  7. If the INHERITS phrase is not specified, class-name-1 forms a new root in the classification scheme. There can be one or more roots in a classification scheme.

  8. The WITH DATA phrase specifies that class-name-1 is able to access directly data inherited from class-name-2.

17.3 Class Extension

A class extension enables you to add extra functionality to an object COBOL class without changing the original source code.

The difference between extending a class with a class extension as opposed to using inheritance is that the class extensions are inherited by all existing subclasses. For example, if class A has a subclass, class B (B INHERITS FROM A), you can add methods to class A by subclassing it to create subclass C. However, class B does not inherit the methods of class C, because it is a subclass of A. If you extend class A with a class extension, X, the effect at run time is the same as if you had changed and recompiled class A: class B inherits all the extra methods in class X.

General Format

Syntax Rules

  1. Extension-name-1 must not be the same as class-name-1.

  2. Extension-name-1 in the END CLASS header must be identical to the extension-name-1 specified in the preceding Class-ID paragraph.

  3. Class-name-1 must be the name of a class specified in the Class-Control paragraph.

  4. The Data Division of the class-body may contain an empty Object-Storage Section. The only other sections that may be specified in the Data Division of a Class Extension are the Working-Storage Section and the Linkage Section.

  5. Statements in the class extension may reference data declared in class-name-1 only when both the following cocnditions are true:

General Rules

  1. The EXTEND clause specifies a class extension. A class extension adds methods to an object class. The methods specified in extension-name-1 are inherited by all subclasses, existing and new, of class-name-1.

  2. During the execution of a run unit, a COBOL call statement to extension-name-1 must be executed before any of the methods in the class extension are invoked. This registers the methods in the class extension with the OO runtime system.

17.4 Class-body

The class body contains all the code that defines class data and class methods.

General Format

Format 1

(Used when the Compiler directive OOCTRL(+N) is specified)


Note: This is the preferred syntax


Format 2

(Used when the Compiler directive OOCTRL(-N) is specified)

Syntax Rules

All Formats
  1. Class-name-3 must not be specified more than once in the Class-Control paragraph.

  2. Class-name-3 may be the same as the class-name specified in the Class-ID paragraph.

  3. The Data Division may not contain a Linkage Section.
Format 1
  1. The Data Division may not contain an Object-Storage Section.

  2. Data items declared in the Data Division may be referenced from instance methods and class methods.
Format 2
  1. Data items declared in the Local-Storage Section, Report Section, and Screen Section may be referenced only from the statements in the Procedure Division of this class, not from any methods.

  2. Data items declared in the File Section or Working-Storage Section can be referenced from instance methods and class methods and from the Procedure Division of this class.

  3. Data items declared in the Object-Storage Section may be referenced from class methods.

General Rules

All Formats
  1. Class-name-3 is implicitly defined as USAGE IS OBJECT REFERENCE.

  2. Class-name-3 is the name of a class that may be used throughout the scope of the containing Environment Division.

  3. External-name-1 specifies the external name of the file containing the class.

  4. Method-1 is a class method.

  5. Only data items in the Object-Storage Section are inherited by subclasses.
Format 1
  1. Data items are in their initial state at the start of the run unit and remain in their last used state between method invocations.
Format 2
  1. The statements in the class Procedure Division are executed before the first time any class method or instance method of this class is executed in the run unit.

  2. When execution of the class Procedure Division begins, the contents of the data items in the Local-Storage Section are undefined. This storage is deallocated immediately after execution of the class Procedure Division.

  3. Data items declared in the File Section and Working-Storage Section remain in their last used state between invocations of class and instance methods.

    Note: Working storage data is useful for class or instance initialization data.


  4. Data items declared in the Object-Storage Section remain in their last used state between invocations of class methods.

17.5 Class-object

The class object is an object that acts as a creator of objects.

General Format

General Rules

  1. If a data-item is declared in the Object-Storage Section of a class object, it is class data. Class data may be referenced only in class methods. Class data can be inherited by subclasses.

  2. Data is in its initial state at the start of the run unit and remains in its last used state between method invocations.

  3. Method-1 is a class method.

17.6 Object-program

The object program contains the definition of data and methods for all instances of the class.

General Format

General Rules

  1. If a data-item is declared in the Object-Storage Section of an object program, it is instance data. Instance data may be referenced only in instance methods.

  2. Data items are in their initial state at the start of the run unit and remain in their last used state between method invocations.

  3. Method-1 is an instance method.

17.7 Method

Format

Syntax Rules

  1. Method-name-1 in the END METHOD header must be identical to the method-name-1 declared in the preceding Method-ID paragraph.

  2. Data declared in the Data Division of a method is accessible only within that method.

  3. Quotation marks around method-name-1 are optional.

    Note: Quotation marks enable you to use reserved words as method-names and to use non-COBOL characters.


  4. A method definition must be contained in a Class Definition.

  5. The format of the PROCEDURE DIVISION header for a method is the same as the format 1 PROCEDURE DIVISION header for a program. The GIVING or RETURNING data-name clause shown in the format 2 header may also be specified. (See the section The PROCEDURE DIVISION Header in the chapter Procedure Division for details.)

General Rules

  1. Method-name-1 names the method declared by this method definition.

  2. Data declared in the Local-Storage Section of a method is allocated separate storage for each invocation of the method and deallocated upon return from the method. This data is in an undefined state each time the method is invoked.

    Note: If you declare your data in the Local-Storage Section, separate invocations of the method do not interfere with each other's data.


  3. Data and files declared in a method in sections other than the Local-Storage Section or the Linkage Section are shared among all invocations of the method and are in their last used state when the method is invoked.

  4. Method-name-1 may be used in a method invocation with an object identifier that references an object containing this method.

  5. If a RETURNING or GIVING phrase is specified in the PROCEDURE DIVISION header of a method, the contents of the data item specified in that phrase when the method terminates become the result of the method. This result is placed in the identifier specified in the RETURNING or GIVING phrase of the INVOKE statement.

17.8 Method Interface Definition

A method interface definition defines the parameters for the method, how they are passed, and any alternate syntax that can be used to invoke the method.

Format

where verb-signature is:

Syntax Rules

  1. A method interface definition must be nested within an external class.

  2. Procedure-division-header is a format 2 procedure-division-header as specified for a program, except that neither mnemonic-name nor the REPEATED phrase may be specified. The INVOKED phrase, shown in the format above, may optionally be included in the header just before the terminating period. (See the section The PROCEDURE DIVISION Header in the chapter Procedure Divisionfor details.)

  3. When the FUNCTION phrase is specified in the verb-signature, the RETURNING phrase must be specified in the PROCEDURE DIVISION header.

  4. Verb-1 must be a COBOL word that is neither a reserved word nor a procedure-name.

  5. The words <OBJECT>, <SELF> and <THIS> are equivalent. One of them must be present once and only once in each verb-signature.

  6. Data-name-3 must be preceded by "<" and followed by ">".

  7. Compulsory-word must be a COBOL word.

  8. Noise-word must be a COBOL word and must be preceded by "[" and followed by "]".

  9. Open-parenthesis and close-parenthesis are "(" and ")" respectively.

    Note: Open-parenthesis and close-parenthesis enable you to define functions that look like intrinsic functions, with parameters enclosed in parentheses.


  10. A verb-signature must not be a subset of another verb-signature.

General Rules

  1. When method-name-1 is invoked using verb-signature, <SELF> is replaced with the object reference to the receiving object.

  2. Noise-words are for readability and are optional when the syntax is used to invoke a method.

  3. If verb-1 is also declared as a data-name in a program, then any reference to that word in that program is to the data-name.

Copyright © 2002 Micro Focus International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.