MainframeMaster

COBOL Tutorial

COBOL INTERFACE - Quick Reference

Progress0 of 0 lessons

Overview

The INTERFACE statement in object-oriented COBOL defines a contract that specifies what methods a class must implement. It provides a way to define a common set of methods that different classes can implement, enabling polymorphism and loose coupling.

Purpose and Usage

  • Define method contracts for classes to implement
  • Enable polymorphism through common interfaces
  • Provide loose coupling between components
  • Support modular design and testing

Syntax

The INTERFACE statement defines method signatures and contracts:

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
INTERFACE-ID. interface-name. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. METHOD-ID. method-name. DATA DIVISION. LINKAGE SECTION. PROCEDURE DIVISION. END METHOD method-name. END INTERFACE interface-name.

Interfaces define method signatures without implementation.

Example: Simple Interface

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
INTERFACE-ID. ICalculator. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. METHOD-ID. Add. DATA DIVISION. LINKAGE SECTION. 01 a PIC 9(5). 01 b PIC 9(5). 01 result PIC 9(6). PROCEDURE DIVISION USING a, b RETURNING result. END METHOD Add. END INTERFACE ICalculator.

Example: Class Implementing Interface

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CLASS-ID. Calculator IMPLEMENTS ICalculator. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. METHOD-ID. Add. DATA DIVISION. LINKAGE SECTION. 01 a PIC 9(5). 01 b PIC 9(5). 01 result PIC 9(6). PROCEDURE DIVISION USING a, b RETURNING result. COMPUTE result = a + b EXIT METHOD. END METHOD Add. END CLASS Calculator.

Practical Examples

Here are some practical uses of INTERFACE in object-oriented COBOL:

Database Interface

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
INTERFACE-ID. IDatabase. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. METHOD-ID. Connect. DATA DIVISION. LINKAGE SECTION. 01 connection-string PIC X(100). 01 success PIC X. PROCEDURE DIVISION USING connection-string RETURNING success. END METHOD Connect. METHOD-ID. ExecuteQuery. DATA DIVISION. LINKAGE SECTION. 01 query PIC X(500). 01 result PIC X(1000). PROCEDURE DIVISION USING query RETURNING result. END METHOD ExecuteQuery. END INTERFACE IDatabase.

Defines a contract for database operations that different database classes can implement.

Logger Interface

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
INTERFACE-ID. ILogger. ENVIRONMENT DIVISION. CONFIGURATION SECTION. OBJECT-COMPUTER. DATA DIVISION. WORKING-STORAGE SECTION. PROCEDURE DIVISION. METHOD-ID. LogInfo. DATA DIVISION. LINKAGE SECTION. 01 message PIC X(200). PROCEDURE DIVISION USING message. END METHOD LogInfo. METHOD-ID. LogError. DATA DIVISION. LINKAGE SECTION. 01 message PIC X(200). PROCEDURE DIVISION USING message. END METHOD LogError. END INTERFACE ILogger.

Defines logging methods that different logger implementations can provide.

Best Practices

  • Define clear, focused interfaces with a single responsibility.
  • Use descriptive names for interfaces and methods.
  • Keep interfaces simple and avoid complex parameter structures.
  • Document the purpose and expected behavior of each method.
  • Use interfaces to enable testing through mock objects.

Common Pitfalls

  • Creating interfaces that are too large or complex.
  • Not implementing all required methods in classes.
  • Forgetting to use IMPLEMENTS keyword in class definitions.
  • Mixing interface and implementation concerns.

Test Your Knowledge

1. What is the primary purpose of the INTERFACE statement in COBOL?

  • To define a user interface
  • To define a contract for object methods
  • To create a file interface
  • To define program parameters

2. In which type of COBOL is INTERFACE primarily used?

  • Standard COBOL
  • Object-oriented COBOL
  • Procedural COBOL
  • Legacy COBOL

3. What does an INTERFACE define?

  • Data structures only
  • Method signatures and contracts
  • File definitions
  • Program logic

4. How do classes use INTERFACE?

  • By inheriting from it
  • By implementing it
  • By calling it
  • By extending it

5. Can an INTERFACE contain data fields?

  • Yes, always
  • No, never
  • Only constants
  • Only static fields

Frequently Asked Questions