MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Inheritance

Inheritance in COBOL is a fundamental object-oriented programming concept that allows a class (subclass or child class) to inherit methods and data from another class (superclass or parent class). Inheritance enables code reuse by allowing subclasses to automatically have access to all public and protected methods and properties of their parent class, while also allowing them to add new functionality or override inherited methods. Understanding inheritance is essential for building maintainable, reusable object-oriented COBOL applications and creating well-structured class hierarchies.

What is Inheritance?

Inheritance is an object-oriented programming mechanism that establishes an "is-a" relationship between classes. When a class inherits from another class, it automatically receives (inherits) all the methods and instance data from the parent class. The subclass can then:

  • Use inherited methods: Call any method defined in the parent class without redefining it
  • Add new methods: Define additional methods specific to the subclass
  • Override methods: Provide a new implementation of an inherited method, replacing or extending the parent's behavior
  • Add new data: Define additional instance data specific to the subclass

Inheritance promotes code reuse, reduces duplication, and enables you to build upon existing functionality rather than rewriting it. It also enables polymorphism, where methods can be called through parent class references but execute subclass-specific implementations.

Basic Inheritance Syntax

In COBOL, inheritance is specified using the INHERITS clause (or INHERITS FROM clause, depending on your compiler) in the CLASS-ID paragraph. The syntax is:

cobol
1
2
3
4
5
CLASS-ID child-class-name INHERITS FROM parent-class-name [FACTORY] [OBJECT] END CLASS child-class-name.

The INHERITS clause must appear immediately after the class name in the CLASS-ID paragraph. Once specified, the child class automatically inherits all methods and instance data from the parent class.

Simple Inheritance Example

Let's look at a basic example showing a parent class and a child class:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
*> Parent class CLASS-ID EMPLOYEE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC X(10). 01 EMPLOYEE-NAME PIC X(50). 01 EMPLOYEE-SALARY PIC 9(8)V99. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID SET-EMPLOYEE-DATA. DATA DIVISION. LINKAGE SECTION. 01 ID PIC X(10). 01 NAME PIC X(50). 01 SALARY PIC 9(8)V99. PROCEDURE DIVISION USING ID, NAME, SALARY. MOVE ID TO EMPLOYEE-ID MOVE NAME TO EMPLOYEE-NAME MOVE SALARY TO EMPLOYEE-SALARY EXIT METHOD. END METHOD SET-EMPLOYEE-DATA. METHOD-ID GET-EMPLOYEE-INFO. DATA DIVISION. LINKAGE SECTION. 01 INFO PIC X(100). PROCEDURE DIVISION RETURNING INFO. STRING EMPLOYEE-ID DELIMITED BY SPACE ' - ' DELIMITED BY SIZE EMPLOYEE-NAME DELIMITED BY SPACE ' - $' DELIMITED BY SIZE EMPLOYEE-SALARY DELIMITED BY SIZE INTO INFO EXIT METHOD. END METHOD GET-EMPLOYEE-INFO. END CLASS EMPLOYEE. *> Child class inheriting from EMPLOYEE CLASS-ID MANAGER INHERITS FROM EMPLOYEE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 DEPARTMENT PIC X(30). 01 TEAM-SIZE PIC 9(3). OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID SET-MANAGER-DATA. DATA DIVISION. LINKAGE SECTION. 01 DEPT PIC X(30). 01 TEAM-COUNT PIC 9(3). PROCEDURE DIVISION USING DEPT, TEAM-COUNT. MOVE DEPT TO DEPARTMENT MOVE TEAM-COUNT TO TEAM-SIZE EXIT METHOD. END METHOD SET-MANAGER-DATA. METHOD-ID GET-MANAGER-INFO. DATA DIVISION. LINKAGE SECTION. 01 MANAGER-INFO PIC X(150). 01 EMPLOYEE-INFO PIC X(100). PROCEDURE DIVISION RETURNING MANAGER-INFO. *> Call inherited method from parent class INVOKE SELF "GET-EMPLOYEE-INFO" RETURNING EMPLOYEE-INFO STRING EMPLOYEE-INFO DELIMITED BY SIZE ' - Dept: ' DELIMITED BY SIZE DEPARTMENT DELIMITED BY SPACE ' - Team: ' DELIMITED BY SIZE TEAM-SIZE DELIMITED BY SIZE INTO MANAGER-INFO EXIT METHOD. END METHOD GET-MANAGER-INFO. END CLASS MANAGER.

In this example:

  • EMPLOYEE is the parent class with methods SET-EMPLOYEE-DATA and GET-EMPLOYEE-INFO
  • MANAGER is the child class that inherits from EMPLOYEE using INHERITS FROM EMPLOYEE
  • MANAGER automatically has access to SET-EMPLOYEE-DATA and GET-EMPLOYEE-INFO from the parent class
  • MANAGER adds new data (DEPARTMENT, TEAM-SIZE) and new methods (SET-MANAGER-DATA, GET-MANAGER-INFO)
  • GET-MANAGER-INFO calls the inherited GET-EMPLOYEE-INFO method using INVOKE SELF

Single Inheritance

COBOL supports only single inheritance, meaning each class can inherit from exactly one parent class. This is similar to Java and C#, but different from languages like C++ that support multiple inheritance (where a class can inherit from multiple parents).

Single inheritance simplifies the language and avoids the complexity and ambiguity issues that can arise with multiple inheritance, such as:

  • The diamond problem: When a class might inherit the same method from multiple parent classes through different paths, creating ambiguity about which implementation to use
  • Name conflicts: When multiple parent classes define methods or data with the same name
  • Complexity: Multiple inheritance can make code harder to understand and maintain

Example: Single Inheritance Chain

Here's an example showing a chain of single inheritance:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
*> Base class CLASS-ID VEHICLE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 VEHICLE-ID PIC X(10). 01 VEHICLE-MAKE PIC X(20). 01 VEHICLE-MODEL PIC X(30). OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID GET-VEHICLE-INFO. DATA DIVISION. LINKAGE SECTION. 01 INFO PIC X(100). PROCEDURE DIVISION RETURNING INFO. STRING VEHICLE-MAKE DELIMITED BY SPACE ' ' DELIMITED BY SIZE VEHICLE-MODEL DELIMITED BY SPACE INTO INFO EXIT METHOD. END METHOD GET-VEHICLE-INFO. END CLASS VEHICLE. *> First level of inheritance CLASS-ID CAR INHERITS FROM VEHICLE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 NUMBER-OF-DOORS PIC 9(1). OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID GET-CAR-INFO. DATA DIVISION. LINKAGE SECTION. 01 CAR-INFO PIC X(120). 01 VEHICLE-INFO PIC X(100). PROCEDURE DIVISION RETURNING CAR-INFO. INVOKE SELF "GET-VEHICLE-INFO" RETURNING VEHICLE-INFO STRING VEHICLE-INFO DELIMITED BY SIZE ' - Doors: ' DELIMITED BY SIZE NUMBER-OF-DOORS DELIMITED BY SIZE INTO CAR-INFO EXIT METHOD. END METHOD GET-CAR-INFO. END CLASS CAR. *> Second level of inheritance CLASS-ID SEDAN INHERITS FROM CAR FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 TRUNK-CAPACITY PIC 9(4). OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID GET-SEDAN-INFO. DATA DIVISION. LINKAGE SECTION. 01 SEDAN-INFO PIC X(150). 01 CAR-INFO PIC X(120). PROCEDURE DIVISION RETURNING SEDAN-INFO. INVOKE SELF "GET-CAR-INFO" RETURNING CAR-INFO STRING CAR-INFO DELIMITED BY SIZE ' - Trunk: ' DELIMITED BY SIZE TRUNK-CAPACITY DELIMITED BY SIZE ' cu ft' DELIMITED BY SIZE INTO SEDAN-INFO EXIT METHOD. END METHOD GET-SEDAN-INFO. END CLASS SEDAN.

In this example:

  • VEHICLE is the base class (no parent)
  • CAR inherits from VEHICLE (first level)
  • SEDAN inherits from CAR (second level)
  • SEDAN inherits from both CAR and VEHICLE (through the chain), but can only directly inherit from one parent (CAR)

Method Overriding

Method overriding occurs when a subclass provides its own implementation of a method that was already defined in the parent class. The overridden method must have the same name and parameter signature as the parent method.

Basic Method Overriding

To override a method, you simply define a method with the same name and signature in the subclass. Some COBOL compilers support an explicit OVERRIDE keyword in the METHOD-ID clause:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*> Parent class CLASS-ID ANIMAL FACTORY. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID MAKE-SOUND. PROCEDURE DIVISION. DISPLAY "Some animal sound" EXIT METHOD. END METHOD MAKE-SOUND. END CLASS ANIMAL. *> Child class overriding the method CLASS-ID DOG INHERITS FROM ANIMAL FACTORY. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID MAKE-SOUND OVERRIDE. PROCEDURE DIVISION. DISPLAY "Woof! Woof!" EXIT METHOD. END METHOD MAKE-SOUND. END CLASS DOG.

When MAKE-SOUND is called on a DOG instance, it executes the DOG version, not the ANIMAL version. The OVERRIDE keyword (if supported) makes the intention explicit and helps catch errors if the method signature doesn't match the parent.

Using SUPER to Extend Parent Behavior

Often, you want to extend the parent method's behavior rather than completely replace it. The SUPER reference allows you to call the parent class version of a method:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
*> Parent class CLASS-ID ACCOUNT FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 ACCOUNT-BALANCE PIC 9(8)V99 VALUE ZERO. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID CALCULATE-INTEREST. DATA DIVISION. LINKAGE SECTION. 01 INTEREST-RATE PIC 9(2)V99. 01 INTEREST-AMOUNT PIC 9(8)V99. PROCEDURE DIVISION USING INTEREST-RATE RETURNING INTEREST-AMOUNT. COMPUTE INTEREST-AMOUNT = ACCOUNT-BALANCE * INTEREST-RATE / 100 EXIT METHOD. END METHOD CALCULATE-INTEREST. END CLASS ACCOUNT. *> Child class extending parent behavior CLASS-ID SAVINGS-ACCOUNT INHERITS FROM ACCOUNT FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 BONUS-RATE PIC 9(2)V99 VALUE 0.5. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID CALCULATE-INTEREST OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 INTEREST-RATE PIC 9(2)V99. 01 INTEREST-AMOUNT PIC 9(8)V99. 01 BASE-INTEREST PIC 9(8)V99. PROCEDURE DIVISION USING INTEREST-RATE RETURNING INTEREST-AMOUNT. *> Call parent method to get base interest INVOKE SUPER "CALCULATE-INTEREST" USING INTEREST-RATE RETURNING BASE-INTEREST *> Add bonus interest COMPUTE INTEREST-AMOUNT = BASE-INTEREST + (BASE-INTEREST * BONUS-RATE / 100) EXIT METHOD. END METHOD CALCULATE-INTEREST. END CLASS SAVINGS-ACCOUNT.

In this example:

  • ACCOUNT has a CALCULATE-INTEREST method that computes basic interest
  • SAVINGS-ACCOUNT overrides CALCULATE-INTEREST
  • The overridden method calls the parent version using INVOKE SUPER to get the base interest
  • Then it adds a bonus interest amount, extending the parent behavior rather than replacing it

Accessing Parent Class Data

In COBOL, instance data (data defined in the WORKING-STORAGE SECTION within the OBJECT paragraph) is typically private to the class where it is defined. This means a subclass cannot directly access instance data from its parent class.

If a subclass needs to access or modify parent class data, it must do so through public methods (getters and setters) provided by the parent class. This maintains proper encapsulation and data integrity.

Example: Accessing Parent Data Through Methods

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
*> Parent class with private data CLASS-ID PERSON FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 PERSON-NAME PIC X(50). 01 PERSON-AGE PIC 9(3). OBJECT SECTION. PROCEDURE DIVISION. *> Getter methods to access private data METHOD-ID GET-NAME. DATA DIVISION. LINKAGE SECTION. 01 NAME PIC X(50). PROCEDURE DIVISION RETURNING NAME. MOVE PERSON-NAME TO NAME EXIT METHOD. END METHOD GET-NAME. METHOD-ID GET-AGE. DATA DIVISION. LINKAGE SECTION. 01 AGE PIC 9(3). PROCEDURE DIVISION RETURNING AGE. MOVE PERSON-AGE TO AGE EXIT METHOD. END METHOD GET-AGE. *> Setter methods to modify private data METHOD-ID SET-NAME. DATA DIVISION. LINKAGE SECTION. 01 NAME PIC X(50). PROCEDURE DIVISION USING NAME. MOVE NAME TO PERSON-NAME EXIT METHOD. END METHOD SET-NAME. METHOD-ID SET-AGE. DATA DIVISION. LINKAGE SECTION. 01 AGE PIC 9(3). PROCEDURE DIVISION USING AGE. MOVE AGE TO PERSON-AGE EXIT METHOD. END METHOD SET-AGE. END CLASS PERSON. *> Child class accessing parent data through methods CLASS-ID EMPLOYEE INHERITS FROM PERSON FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC X(10). OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID GET-EMPLOYEE-INFO. DATA DIVISION. LINKAGE SECTION. 01 INFO PIC X(150). 01 NAME PIC X(50). 01 AGE PIC 9(3). PROCEDURE DIVISION RETURNING INFO. *> Access parent data through inherited methods INVOKE SELF "GET-NAME" RETURNING NAME INVOKE SELF "GET-AGE" RETURNING AGE STRING EMPLOYEE-ID DELIMITED BY SPACE ' - ' DELIMITED BY SIZE NAME DELIMITED BY SPACE ' (' DELIMITED BY SIZE AGE DELIMITED BY SIZE ')' DELIMITED BY SIZE INTO INFO EXIT METHOD. END METHOD GET-EMPLOYEE-INFO. END CLASS EMPLOYEE.

In this example, EMPLOYEE cannot directly access PERSON-NAME or PERSON-AGE, but it can use the inherited GET-NAME and GET-AGE methods to retrieve the values. This maintains encapsulation while still allowing subclasses to work with parent class data.

Inheritance Hierarchies

An inheritance hierarchy is a tree-like structure of classes where each class (except the root) inherits from exactly one parent class, forming chains of inheritance. Well-designed hierarchies follow the "is-a" relationship principle, where a subclass represents a more specific type of its parent class.

Example: Employee Hierarchy

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
*> Base class CLASS-ID EMPLOYEE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMP-ID PIC X(10). 01 EMP-NAME PIC X(50). 01 BASE-SALARY PIC 9(8)V99. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID CALCULATE-SALARY. DATA DIVISION. LINKAGE SECTION. 01 SALARY PIC 9(8)V99. PROCEDURE DIVISION RETURNING SALARY. MOVE BASE-SALARY TO SALARY EXIT METHOD. END METHOD CALCULATE-SALARY. END CLASS EMPLOYEE. *> First level subclass CLASS-ID MANAGER INHERITS FROM EMPLOYEE FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 BONUS-AMOUNT PIC 9(8)V99. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID CALCULATE-SALARY OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 SALARY PIC 9(8)V99. 01 BASE-SAL PIC 9(8)V99. PROCEDURE DIVISION RETURNING SALARY. INVOKE SUPER "CALCULATE-SALARY" RETURNING BASE-SAL COMPUTE SALARY = BASE-SAL + BONUS-AMOUNT EXIT METHOD. END METHOD CALCULATE-SALARY. END CLASS MANAGER. *> Second level subclass CLASS-ID SENIOR-MANAGER INHERITS FROM MANAGER FACTORY. DATA DIVISION. WORKING-STORAGE SECTION. 01 STOCK-OPTIONS PIC 9(8)V99. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID CALCULATE-SALARY OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 SALARY PIC 9(8)V99. 01 MANAGER-SAL PIC 9(8)V99. PROCEDURE DIVISION RETURNING SALARY. INVOKE SUPER "CALCULATE-SALARY" RETURNING MANAGER-SAL COMPUTE SALARY = MANAGER-SAL + STOCK-OPTIONS EXIT METHOD. END METHOD CALCULATE-SALARY. END CLASS SENIOR-MANAGER.

This hierarchy creates: EMPLOYEE → MANAGER → SENIOR-MANAGER. Each level adds more specific functionality:

  • EMPLOYEE: Base salary calculation
  • MANAGER: Adds bonus to base salary
  • SENIOR-MANAGER: Adds stock options to manager salary

Best Practices for Inheritance

Follow these best practices when using inheritance in COBOL:

  • Follow the "is-a" relationship: A subclass should represent a more specific type of its parent class. If the relationship is "has-a" instead of "is-a," consider using composition instead of inheritance.
  • Use inheritance for code reuse: Inheritance is most valuable when you have common functionality that multiple classes can share. Don't use inheritance just to access a few methods—consider composition or utility classes instead.
  • Keep hierarchies shallow: Deep inheritance hierarchies (many levels) can be hard to understand and maintain. Prefer shallow hierarchies with clear relationships.
  • Use SUPER for extension: When overriding methods, use SUPER to call the parent implementation when you want to extend behavior rather than completely replace it. This maintains the parent's functionality while adding new features.
  • Provide accessor methods: If subclasses need to access parent data, provide getter and setter methods in the parent class rather than making data directly accessible. This maintains encapsulation.
  • Document inheritance relationships: Clearly document why a class inherits from another and what additional functionality it provides. This helps other developers understand the design.
  • Avoid deep method call chains: While SUPER is useful, avoid creating long chains of method calls through multiple inheritance levels, as this can impact performance and make debugging difficult.
  • Consider composition for flexibility: Sometimes composition (including objects as data members) is more flexible than inheritance. Use inheritance when you need polymorphism and code reuse; use composition when you need more dynamic relationships.

Common Inheritance Patterns

Pattern 1: Template Method Pattern

The parent class defines the structure of an algorithm, and subclasses provide specific implementations:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
CLASS-ID DATA-PROCESSOR FACTORY. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID PROCESS-DATA. PROCEDURE DIVISION. PERFORM VALIDATE-DATA PERFORM TRANSFORM-DATA PERFORM SAVE-DATA EXIT METHOD. END METHOD PROCESS-DATA. METHOD-ID VALIDATE-DATA. PROCEDURE DIVISION. *> Default validation - can be overridden EXIT METHOD. END METHOD VALIDATE-DATA. METHOD-ID TRANSFORM-DATA. PROCEDURE DIVISION. *> Default transformation - can be overridden EXIT METHOD. END METHOD TRANSFORM-DATA. METHOD-ID SAVE-DATA. PROCEDURE DIVISION. *> Default save - can be overridden EXIT METHOD. END METHOD SAVE-DATA. END CLASS DATA-PROCESSOR.

Pattern 2: Specialization

Subclasses specialize parent behavior by adding specific functionality:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
CLASS-ID PAYMENT-PROCESSOR FACTORY. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID PROCESS-PAYMENT. DATA DIVISION. LINKAGE SECTION. 01 AMOUNT PIC 9(8)V99. PROCEDURE DIVISION USING AMOUNT. *> Base payment processing EXIT METHOD. END METHOD PROCESS-PAYMENT. END CLASS PAYMENT-PROCESSOR. CLASS-ID CREDIT-CARD-PROCESSOR INHERITS FROM PAYMENT-PROCESSOR FACTORY. OBJECT SECTION. PROCEDURE DIVISION. METHOD-ID PROCESS-PAYMENT OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 AMOUNT PIC 9(8)V99. PROCEDURE DIVISION USING AMOUNT. *> Credit card specific processing INVOKE SUPER "PROCESS-PAYMENT" USING AMOUNT *> Add credit card validation, authorization, etc. EXIT METHOD. END METHOD PROCESS-PAYMENT. END CLASS CREDIT-CARD-PROCESSOR.

Explain Like I'm 5: Inheritance

Think of inheritance like a family tree:

  • Parent class is like a grandparent who knows how to do certain things (like cook, drive, or tell stories)
  • Child class is like a child who automatically knows how to do everything the grandparent knows, plus can learn new things
  • Method overriding is like when the child learns to do something the same way but better—like the grandparent cooks with a recipe, but the child adds their own special ingredient
  • SUPER reference is like asking the grandparent to do their version first, and then the child adds their own touch

So inheritance is like getting a head start—you automatically know everything your parent knows, and then you can add your own special skills on top!

Practice Exercises

Complete these exercises to reinforce your understanding of inheritance:

Exercise 1: Create a Shape Hierarchy

Create a base SHAPE class with a method to calculate area. Then create RECTANGLE and CIRCLE classes that inherit from SHAPE and override the area calculation method with their specific formulas.

Exercise 2: Employee Management System

Create an EMPLOYEE base class with name and ID. Create PART-TIME-EMPLOYEE and FULL-TIME-EMPLOYEE subclasses that inherit from EMPLOYEE and add their own specific data (hours worked, salary type, etc.). Override a method to calculate pay differently for each type.

Exercise 3: Using SUPER

Create a VEHICLE class with a START-ENGINE method that displays "Engine starting...". Create a CAR class that inherits from VEHICLE and overrides START-ENGINE to call the parent method using SUPER, then add "Car is ready to drive!".

Exercise 4: Multi-Level Inheritance

Create a three-level inheritance hierarchy: ANIMAL → MAMMAL → DOG. Each level should add new methods and data. Show how DOG can call methods from both MAMMAL and ANIMAL.

Exercise 5: Accessor Methods

Create a PARENT class with private data and provide getter and setter methods. Create a CHILD class that inherits from PARENT and uses the accessor methods to work with the parent's data, demonstrating proper encapsulation.

Test Your Knowledge

1. How is inheritance specified in a COBOL class?

  • Using the EXTENDS keyword in the PROCEDURE DIVISION
  • Using the INHERITS clause in the CLASS-ID paragraph
  • Using the INHERITANCE section in the DATA DIVISION
  • Using the PARENT-OF statement in the METHOD-ID paragraph

2. How many parent classes can a COBOL class inherit from?

  • Unlimited
  • Two
  • One
  • Zero

3. What is the SUPER reference used for in COBOL inheritance?

  • To create new instances of the parent class
  • To invoke methods from the parent class within a subclass
  • To access private data from the parent class
  • To define the parent class

4. What happens when a subclass defines a method with the same name as a parent class method?

  • A compilation error occurs
  • The parent method is automatically deleted
  • The subclass method overrides the parent method
  • Both methods are called simultaneously

5. Can a subclass directly access instance data from its parent class?

  • Yes, always
  • No, instance data is private to each class
  • Only if the data is marked as PUBLIC
  • Only if the data is marked as PROTECTED

6. What is the purpose of an inheritance hierarchy?

  • To organize classes alphabetically
  • To create a tree-like structure where classes inherit from parent classes, enabling code reuse and specialization
  • To improve compilation speed
  • To reduce memory usage

Related Concepts

Related Pages