Object-Oriented COBOL (OO-COBOL) is an extension of the traditional COBOL language that incorporates object-oriented programming (OOP) principles. This module will introduce you to the key concepts of OOP in COBOL, including classes, objects, inheritance, and polymorphism.

Key Concepts of Object-Oriented Programming

Before diving into OO-COBOL, let's briefly review the fundamental concepts of OOP:

  1. Classes and Objects:

    • Class: A blueprint for creating objects. It defines a set of attributes and methods that the objects created from the class will have.
    • Object: An instance of a class. It contains data (attributes) and methods (functions or procedures) defined by the class.
  2. Encapsulation:

    • The bundling of data and methods that operate on the data within a single unit (class). It restricts direct access to some of the object's components, which is a means of preventing accidental interference and misuse of the data.
  3. Inheritance:

    • A mechanism where a new class (subclass) inherits attributes and methods from an existing class (superclass). This promotes code reuse and establishes a natural hierarchy between classes.
  4. Polymorphism:

    • The ability of different classes to be treated as instances of the same class through a common interface. It allows methods to be used interchangeably, even if they belong to different classes.

Object-Oriented Features in COBOL

COBOL has been extended to support OOP features. Here are the key elements of OO-COBOL:

  1. Class Definition

In OO-COBOL, a class is defined using the CLASS-ID keyword. The class definition includes data and method sections.

CLASS-ID. Employee.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 EmployeeID    PIC 9(5).
01 EmployeeName  PIC X(30).

PROCEDURE DIVISION.
METHOD-ID. "SetEmployeeDetails".
    PROCEDURE DIVISION USING BY VALUE empID AS PIC 9(5), empName AS PIC X(30).
    MOVE empID TO EmployeeID.
    MOVE empName TO EmployeeName.
    END METHOD "SetEmployeeDetails".

METHOD-ID. "DisplayEmployeeDetails".
    PROCEDURE DIVISION.
    DISPLAY "Employee ID: " EmployeeID.
    DISPLAY "Employee Name: " EmployeeName.
    END METHOD "DisplayEmployeeDetails".

END CLASS Employee.

  1. Creating Objects

Objects are created using the NEW keyword and are typically managed through object references.

IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 EmployeeObj OBJECT REFERENCE Employee.

PROCEDURE DIVISION.
    CREATE Employee NEW EmployeeObj.
    INVOKE EmployeeObj "SetEmployeeDetails" USING 12345 "John Doe".
    INVOKE EmployeeObj "DisplayEmployeeDetails".
    STOP RUN.

  1. Inheritance

Inheritance in OO-COBOL is achieved using the INHERITS keyword.

CLASS-ID. Manager INHERITS Employee.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 Department PIC X(20).

PROCEDURE DIVISION.
METHOD-ID. "SetDepartment".
    PROCEDURE DIVISION USING BY VALUE dept AS PIC X(20).
    MOVE dept TO Department.
    END METHOD "SetDepartment".

METHOD-ID. "DisplayManagerDetails".
    PROCEDURE DIVISION.
    INVOKE SELF "DisplayEmployeeDetails".
    DISPLAY "Department: " Department.
    END METHOD "DisplayManagerDetails".

END CLASS Manager.

  1. Polymorphism

Polymorphism is supported through method overriding and dynamic method invocation.

CLASS-ID. Contractor INHERITS Employee.

PROCEDURE DIVISION.
METHOD-ID. "DisplayEmployeeDetails" OVERRIDE.
    PROCEDURE DIVISION.
    DISPLAY "Contractor ID: " EmployeeID.
    DISPLAY "Contractor Name: " EmployeeName.
    END METHOD "DisplayEmployeeDetails".

END CLASS Contractor.

Practical Exercise

Exercise 1: Define and Use a Class

  1. Define a class Product with attributes ProductID and ProductName.
  2. Add methods to set and display the product details.
  3. Create an object of the Product class in a COBOL program and use the methods to set and display the product details.

Solution

CLASS-ID. Product.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 ProductID    PIC 9(5).
01 ProductName  PIC X(30).

PROCEDURE DIVISION.
METHOD-ID. "SetProductDetails".
    PROCEDURE DIVISION USING BY VALUE prodID AS PIC 9(5), prodName AS PIC X(30).
    MOVE prodID TO ProductID.
    MOVE prodName TO ProductName.
    END METHOD "SetProductDetails".

METHOD-ID. "DisplayProductDetails".
    PROCEDURE DIVISION.
    DISPLAY "Product ID: " ProductID.
    DISPLAY "Product Name: " ProductName.
    END METHOD "DisplayProductDetails".

END CLASS Product.

IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 ProductObj OBJECT REFERENCE Product.

PROCEDURE DIVISION.
    CREATE Product NEW ProductObj.
    INVOKE ProductObj "SetProductDetails" USING 10001 "Laptop".
    INVOKE ProductObj "DisplayProductDetails".
    STOP RUN.

Exercise 2: Implement Inheritance

  1. Define a subclass Electronics that inherits from Product.
  2. Add an attribute WarrantyPeriod to the Electronics class.
  3. Override the DisplayProductDetails method to include the warranty period.
  4. Create an object of the Electronics class and use the methods to set and display the product details along with the warranty period.

Solution

CLASS-ID. Electronics INHERITS Product.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 WarrantyPeriod PIC 9(2).

PROCEDURE DIVISION.
METHOD-ID. "SetWarrantyPeriod".
    PROCEDURE DIVISION USING BY VALUE warranty AS PIC 9(2).
    MOVE warranty TO WarrantyPeriod.
    END METHOD "SetWarrantyPeriod".

METHOD-ID. "DisplayProductDetails" OVERRIDE.
    PROCEDURE DIVISION.
    INVOKE SELF "DisplayProductDetails" OF Product.
    DISPLAY "Warranty Period: " WarrantyPeriod " months".
    END METHOD "DisplayProductDetails".

END CLASS Electronics.

IDENTIFICATION DIVISION.
PROGRAM-ID. MainProgram.

DATA DIVISION.
WORKING-STORAGE SECTION.
01 ElectronicsObj OBJECT REFERENCE Electronics.

PROCEDURE DIVISION.
    CREATE Electronics NEW ElectronicsObj.
    INVOKE ElectronicsObj "SetProductDetails" USING 20001 "Smartphone".
    INVOKE ElectronicsObj "SetWarrantyPeriod" USING 24.
    INVOKE ElectronicsObj "DisplayProductDetails".
    STOP RUN.

Conclusion

In this module, you have learned the basics of Object-Oriented COBOL, including class definitions, object creation, inheritance, and polymorphism. These concepts allow you to write more modular, reusable, and maintainable COBOL code. In the next module, we will explore how COBOL interacts with databases, which is crucial for developing enterprise-level applications.

© Copyright 2024. All rights reserved