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.
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.
- ALIGN - specifies the memory boundaries on which data items of
01-level or 77-level are aligned.
- IBMCOMP - turns on word-storage mode.
- MAPNAME - impacts the handling of non-alphabetic characters in the
method-name.
- MF-OO - enables the syntax used for OO
- REPOSITORY - tells the Compiler whether to create a repository entry
for a class, ignore the repository entry for a class, or check against the
repository entry for a class.
- OOCTRL - provides specific switches for OO options. The settings can
be enabled or disabled by use of the + and - signs, respectively, preceding the
option character. The default is OOCTRL(+F-G-P+Q-W). The meaning of the
options is:
+F |
Folds method
names invoked by the program being compiled to lower case. Use -F if your
program calls Java methods, since Java method names are case-sensitive. See
your COBOL system documentation for information on using Java and COBOL
together. |
+G |
Makes class data global for instances.
Note: The use of this option is not recommended. It is
provided for compatibility with earlier releases.
|
+P |
Makes parameter type information available to the Object COBOL
runtime system.
Note: Needed for messages sent to OLE and SOM.
|
+Q |
Disables the use of IN and OF any place that could possibly
follow a data name in the verb-signature of a Method Interface Definition.
Disables the use of any verb in the verb-signature of a
Method Interface Definition (see the section
Categories of
Statements in the chapter Language Fundamentals).
|
-Q |
Enables the use of IN and OF in the verb-signature of a Method
Interface Definition. Disables the use of qualification in a verb-signature
that invokes a method.
Enables the use of verbs in the verb-signature of a Method
Interface Definition (see the section
Categories of
Statements in the chapter Language Fundamentals).
|
+W |
Uses Working-Storage in the object and class object to mean
Object-Storage.
Note: Provides compatibility with the developing ISO
COBOL standard.
|
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
- The words PROTECTED and RESTRICTED are equivalent.
- If the EXTERNAL clause is specified, neither the DATA nor the
INHERITS clause may be specified.
- 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.
- Class-name-2 must not be the same as class-name-1.
- Class-name-2 must not inherit from class-name-1 directly or
indirectly.
- 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
- Class-name-1 identifies the class being declared.
- The ABSTRACT clause indicates that class-name-1 is an abstract class.
You cannot create an instance of an abstract class.
- The EXTERNAL phrase indicates that class-name-1 is an external class.
No code is produced.
- The RESTRICTED phrase enables subclasses to directly access data
inherited from class-name-1.
- The PRIVATE phrase prevents subclasses from directly accessing data
inherited from class-name-1.
- The INHERITS phrase specifies that class-name-2 is a parent class of
class-name-1.
- 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.
- The WITH DATA phrase specifies that class-name-1 is able to access
directly data inherited from class-name-2.
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
- Extension-name-1 must not be the same as class-name-1.
- Extension-name-1 in the END CLASS header must be identical to the
extension-name-1 specified in the preceding
Class-ID paragraph.
- Class-name-1 must be the name of a class specified in the
Class-Control
paragraph.
- 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.
- Statements in the class extension may reference data declared in
class-name-1 only when both
the following cocnditions are true:
- The DATA IS PROTECTED or DATA IS RESTRICTED phrase is specified
in the Class-ID
paragraph of class-name-1
- The WITH DATA phrase is specified in the
Class-ID paragraph
of the class extension.
General Rules
- 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.
- 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.
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
- Class-name-3 must not be specified more than once in the
Class-Control
paragraph.
- Class-name-3 may be the same as the class-name specified in the
Class-ID paragraph.
- The Data Division may not contain a Linkage Section.
Format 1
- The Data Division may not contain an Object-Storage Section.
- Data items declared in the Data Division may be referenced from
instance methods and class methods.
Format 2
- 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.
- 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.
- Data items declared in the Object-Storage Section may be referenced
from class methods.
General Rules
All Formats
- Class-name-3 is implicitly defined as USAGE IS OBJECT REFERENCE.
- Class-name-3 is the name of a class that may be used throughout the
scope of the containing Environment Division.
- External-name-1 specifies the external name of the file containing
the class.
- Method-1 is a class method.
- Only data items in the Object-Storage Section are inherited by
subclasses.
Format 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
- 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.
- 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.
- 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.
- Data items declared in the Object-Storage Section remain in their
last used state between invocations of class methods.
The class object is an object that acts as a creator of objects.
General Format
General Rules
- 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.
- Data is in its initial state at the start of the run unit and remains
in its last used state between method invocations.
- Method-1 is a class method.
The object program contains the definition of data and methods for all
instances of the class.
General Format
General Rules
- 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.
- Data items are in their initial state at the start of the run unit
and remain in their last used state between method invocations.
- Method-1 is an instance method.
Format
Syntax Rules
- Method-name-1 in the END METHOD header must be identical to the
method-name-1 declared in the preceding
Method-ID
paragraph.
- Data declared in the Data Division of a method is accessible only
within that method.
- 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.
- A method definition must be contained in a Class Definition.
- 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
- Method-name-1 names the method declared by this method definition.
- 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.
- 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.
- Method-name-1 may be used in a method invocation with an object
identifier that references an object containing this method.
- 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.
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
- A method interface definition must be nested within an external
class.
- 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.)
- When the FUNCTION phrase is specified in the verb-signature, the
RETURNING phrase must be specified in the
PROCEDURE DIVISION
header.
- Verb-1 must be a COBOL word that is neither a reserved word nor a
procedure-name.
- The words <OBJECT>, <SELF> and <THIS> are
equivalent. One of them must be present once and only once in each
verb-signature.
- Data-name-3 must be preceded by "<" and followed by
">".
- Compulsory-word must be a COBOL word.
- Noise-word must be a COBOL word and must be preceded by "["
and followed by "]".
- 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.
- A verb-signature must not be a subset of another verb-signature.
General Rules
- When method-name-1 is invoked using verb-signature, <SELF> is
replaced with the object reference to the receiving object.
- Noise-words are for readability and are optional when the syntax is
used to invoke a method.
- 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.