OO COBOL: inheritance and interfaces
Inheritance and interfaces are the two clauses that make OO COBOL worth the trouble: INHERITS FROM takes the data and the methods of another class, IMPLEMENTS promises a set of methods that a caller can rely on. Neither clause is reachable here, and the two ways a plain COBOL program reaches the same effect are worth knowing, because they are what a shop actually runs.
INHERITS and IMPLEMENTS
The clauses sit on the CLASS-ID line, which is the first thing in the file:
IDENTIFICATION DIVISION.
CLASS-ID. SAVINGS INHERITS FROM ACCOUNT IMPLEMENTS IFACE.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
CLASS ACCOUNT.
INTERFACE IFACE.
IDENTIFICATION DIVISION.
FACTORY.
PROCEDURE DIVISION.
END FACTORY.
OBJECT.
PROCEDURE DIVISION.
END OBJECT.
END CLASS SAVINGS.
cobc -free -Wall -c savings.cob
savings.cob:2: error: 'CLASS-ID' is a reserved word, but isn't supported
1 | IDENTIFICATION DIVISION.
2 > CLASS-ID. SAVINGS INHERITS FROM ACCOUNT IMPLEMENTS IFACE.
3 | ENVIRONMENT DIVISION.
4 | CONFIGURATION SECTION.
savings.cob:2: error: syntax error, unexpected Identifier, expecting FUNCTION-ID or PROGRAM-ID
The error is the one of the previous lesson and it is the honest statement about the clauses: the compiler never parsed INHERITS or IMPLEMENTS, because it stopped at the word before them. A page that showed a build of inheritance would be inventing an output.
What the two clauses mean is still worth a paragraph. An inherited class takes the instance data and the method bodies of its parent and may replace a method with its own, so a caller of the interface of the parent gets the behaviour of the child. An interface is a list of method names and parameter lists with no data, and a class that implements it promises to answer every name on that list. The point of both is that the caller writes one call and the object decides what runs.
Two programs, one interface
A COBOL program gets that decision from the name in a CALL, which may be a variable. Two subprograms with one parameter list are two implementations of one interface, and the table of names is the choice of which one runs:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAINP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 T-KIND.
05 T-TYPE PIC X(4) OCCURS 2 TIMES.
01 WS-I PIC 9 VALUE 1.
01 WS-PROG PIC X(8).
01 WS-AMOUNT PIC 9(7).
01 WS-RESULT PIC 9(9).
PROCEDURE DIVISION.
MOVE "SAVE" TO T-TYPE(1).
MOVE "CURR" TO T-TYPE(2).
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I IS GREATER THAN 2
MOVE T-TYPE(WS-I) TO WS-PROG
MOVE 1000 TO WS-AMOUNT
CALL WS-PROG USING WS-AMOUNT WS-RESULT
DISPLAY T-TYPE(WS-I) " interest " WS-RESULT
END-PERFORM.
STOP RUN.
END PROGRAM MAINP.
IDENTIFICATION DIVISION.
PROGRAM-ID. SAVE.
DATA DIVISION.
LINKAGE SECTION.
01 L-AMOUNT PIC 9(7).
01 L-RESULT PIC 9(9).
PROCEDURE DIVISION USING L-AMOUNT L-RESULT.
COMPUTE L-RESULT = L-AMOUNT * 2 / 100
GOBACK.
END PROGRAM SAVE.
IDENTIFICATION DIVISION.
PROGRAM-ID. CURR.
DATA DIVISION.
LINKAGE SECTION.
01 L-AMOUNT PIC 9(7).
01 L-RESULT PIC 9(9).
PROCEDURE DIVISION USING L-AMOUNT L-RESULT.
COMPUTE L-RESULT = L-AMOUNT * 5 / 1000
GOBACK.
END PROGRAM CURR.
cobc -x -free -Wall -o poly poly.cob
./poly
SAVE interest 000000020
CURR interest 000000005
The caller wrote one call and the data chose the program: 2 per cent for the savings account and 0.5 per cent for the current one. This is a virtual method with the dispatch table spelled out in a table of names, and it has the property the OO version has: a third account type is a third subprogram and one more name in the table, with no change in the caller.
The weakness is the same as its strength. The name is not checked at compile time, so a type that does not match a subprogram fails at run time with the module-not-found message of the call lesson, and the parameter list must match the one the callee expects, which no compiler checks.
A type field and EVALUATE
The other way keeps everything in one program: the type is a field of the record, and EVALUATE on it is the dispatch:
IDENTIFICATION DIVISION.
PROGRAM-ID. DISP.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 T-ACC.
05 T-ENTRY OCCURS 3 TIMES.
10 T-TYPE PIC X(4).
10 T-BALANCE PIC 9(7).
01 WS-I PIC 9 VALUE 1.
01 WS-RATE PIC 9V999.
01 WS-INTEREST PIC 9(7).
01 WS-TOTAL PIC 9(9) VALUE 0.
PROCEDURE DIVISION.
MOVE "SAVE" TO T-TYPE(1).
MOVE 1000 TO T-BALANCE(1).
MOVE "CURR" TO T-TYPE(2).
MOVE 1000 TO T-BALANCE(2).
MOVE "SAVE" TO T-TYPE(3).
MOVE 500 TO T-BALANCE(3).
PERFORM VARYING WS-I FROM 1 BY 1 UNTIL WS-I IS GREATER THAN 3
EVALUATE T-TYPE(WS-I)
WHEN "SAVE" MOVE 0.020 TO WS-RATE
WHEN "CURR" MOVE 0.005 TO WS-RATE
WHEN OTHER MOVE 0 TO WS-RATE
END-EVALUATE
COMPUTE WS-INTEREST = T-BALANCE(WS-I) * WS-RATE
ADD WS-INTEREST TO WS-TOTAL
DISPLAY T-TYPE(WS-I) " " T-BALANCE(WS-I)
" interest " WS-INTEREST
END-PERFORM.
DISPLAY "total interest " WS-TOTAL.
STOP RUN.
cobc -x -free -Wall -o dispatch dispatch.cob
./dispatch
SAVE 0001000 interest 0000020
CURR 0001000 interest 0000005
SAVE 0000500 interest 0000010
total interest 000000035
The same two rates and the same three accounts, computed in one program with one loop. A new type is one more WHEN, and every branch is in one place, which is what makes this shape win in a shop that deals in a handful of types rather than a hierarchy of them.
Both shapes are honest answers to a question the language of this compiler cannot answer with a class. The next lesson collects the OO words and the message each one gets, so that the boundary is written down once.
Exercises
- Add a third account type to the table of the second program and to the EVALUATE of the third, and compare the work.
- Change the name in the table of the second program to "FIXED" and run it. Which message comes out?
- Make the rate of the EVALUATE program come from a table of rates instead of a MOVE in each branch.
- Add a fifth call to the account subprogram of the previous lesson with an operation it does not know, and read what it does.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does INHERITS FROM say in OO COBOL?
Answer
That the class takes the data and methods of another class
The page describes the inherited data and replaced methods, and the compiler never reaches the clause.
-
What does IMPLEMENTS promise?
Answer
That the class answers every method name of an interface
An interface is a list of method names and parameter lists with no data.
-
How does the second program choose which subprogram runs?
Answer
The name in a CALL comes from a table of type names
WS-PROG held the type name and the CALL used the variable.
-
What does the third program use for the dispatch?
Answer
A type field of the record and EVALUATE on it
The type of each account chose the rate in one EVALUATE.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-B PIC 9(7) VALUE 1000. 01 W-R PIC 9(7). PROCEDURE DIVISION. COMPUTE W-R = W-B * 2 / 100 DISPLAY W-R. STOP RUN.Answer
0000020
Two per cent of a thousand is twenty.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q6. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-T PIC X(4) VALUE "CURR". 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. EVALUATE W-T WHEN "SAVE" MOVE 2 TO W-N WHEN "CURR" MOVE 5 TO W-N WHEN OTHER MOVE 9 TO W-N END-EVALUATE. DISPLAY W-N. STOP RUN.Answer
5
The current account branch answers five.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q7. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-T PIC X(4) VALUE "FIXED". 01 W-N PIC 9 VALUE 0. PROCEDURE DIVISION. EVALUATE W-T WHEN "SAVE" MOVE 2 TO W-N WHEN "CURR" MOVE 5 TO W-N WHEN OTHER MOVE 9 TO W-N END-EVALUATE. DISPLAY W-N. STOP RUN.Answer
9
A type with no branch falls to WHEN OTHER.
-
What does this program print?
IDENTIFICATION DIVISION. PROGRAM-ID. Q8. DATA DIVISION. WORKING-STORAGE SECTION. 01 W-A PIC 9(4) VALUE 20. 01 W-B PIC 9(4) VALUE 10. 01 W-T PIC 9(5) VALUE 0. PROCEDURE DIVISION. ADD W-A TO W-T ADD W-B TO W-T DISPLAY W-T. STOP RUN.Answer
00030
The two interests add to thirty in a five-digit field.
-
What is the weakness of dispatch by CALL name?
Answer
The name is resolved at run time, so a wrong type fails with module not found
The compiler cannot see which program a variable name will hold.
-
Why does a COBOL shop accept a type field instead of inheritance?
Answer
All the branches are in one place, and a new type is one more WHEN
The third program keeps every rate in one EVALUATE.
The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev