Object-Oriented COBOL

COBOL 2002 standardized classes, objects, methods, inheritance, interfaces, and INVOKE.

29.1 Overview

COBOL 2002 standardized classes, objects, methods, inheritance, interfaces, and INVOKE. Support varies:

IBM Enterprise COBOL OO syntax mainly for Java interoperability (classes inherit from java.lang.Object; runs with the JVM under z/OS UNIX / batch / CICS with restrictions) Rocket Visual COBOL (Micro Focus)

Full OO COBOL, compiled to .NET or JVM classes with an extended, more concise syntax

Parses some OO syntax but does not implement OO COBOL

29.2 A class (IBM Enterprise COBOL style)

IDENTIFICATION DIVISION.
       CLASS-ID. Account INHERITS Base.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.
       REPOSITORY.
           CLASS Base    IS "java.lang.Object"
           CLASS Account IS "Account".

       IDENTIFICATION DIVISION.
       OBJECT.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  AccountBalance       PIC S9(9) BINARY VALUE 0.
       PROCEDURE DIVISION.

       IDENTIFICATION DIVISION.
       METHOD-ID. "credit".
       DATA DIVISION.
       LINKAGE SECTION.
       01  inAmount             PIC S9(9) BINARY.
       PROCEDURE DIVISION USING BY VALUE inAmount.
           ADD inAmount TO AccountBalance.
       END METHOD "credit".

      * A "getBalance" method using RETURNING follows the same pattern
       END OBJECT.
       END CLASS Account.

29.3 A client

The client declares the class in its REPOSITORY paragraph (CLASS Account IS "Account"), then:

In Visual COBOL the same ideas use a more modern syntax (class-id, method-id, properties, set acct to new Account(), invoke acct::credit(500)). In practice, most enterprises integrate COBOL with Java through APIs or IBM's COBOL–Java interoperability features rather than writing class hierarchies in COBOL (Chapter 40).

01  anAccount            USAGE OBJECT REFERENCE Account.
       01  WS-BALANCE           PIC S9(9) BINARY.
      * ...
           INVOKE Account "NEW" RETURNING anAccount
           INVOKE anAccount "credit" USING BY VALUE 500
           INVOKE anAccount "getBalance" RETURNING WS-BALANCE