MainframeMaster

COBOL Tutorial

COBOL OVERRIDE - Method Overriding

Progress0 of 0 lessons

What is OVERRIDE?

OVERRIDE is a keyword in object-oriented COBOL that allows a subclass to provide a new implementation of a method that is already defined in its parent class. Think of it as "customizing" inherited behavior - you're taking a method from your parent class and giving it your own special implementation while keeping the same interface.

🎭 Real-World Analogy

Imagine you're running a restaurant chain:

  • Parent Class: The main restaurant (has a standard "cook" method)
  • Subclass: A pizza restaurant (inherits from main restaurant)
  • OVERRIDE: The pizza restaurant provides its own "cook" method for making pizza
  • Polymorphism: When someone asks any restaurant to "cook", each one does it their way

OVERRIDE lets each restaurant type customize how they cook while keeping the same "cook" interface.

Key Concepts

  • Inheritance - A subclass inherits methods from its parent class
  • Method Overriding - Replacing an inherited method with a new implementation
  • Polymorphism - Using the same method name for different behaviors
  • Liskov Substitution - Subclasses can be used wherever parent classes are expected
  • Method Signature - The name, parameters, and return type must match exactly
  • Runtime Resolution - The actual method called depends on the object type at runtime

How to Use OVERRIDE

Using OVERRIDE involves creating a subclass that inherits from a parent class and then providing your own implementation of inherited methods. The OVERRIDE keyword tells COBOL that you're intentionally replacing the parent's method.

Basic Method Override

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
* Basic method override example IDENTIFICATION DIVISION. PROGRAM-ID. OVERRIDE-EXAMPLE. * Parent class definition CLASS-ID. Animal. METHOD-ID. makeSound. DISPLAY "Some animal sound" EXIT METHOD. END METHOD makeSound. END CLASS Animal. * Subclass with override CLASS-ID. Dog INHERITS FROM Animal. METHOD-ID. makeSound OVERRIDE. DISPLAY "Woof! Woof!" EXIT METHOD. END METHOD makeSound. END CLASS Dog. * Another subclass with different override CLASS-ID. Cat INHERITS FROM Animal. METHOD-ID. makeSound OVERRIDE. DISPLAY "Meow! Meow!" EXIT METHOD. END METHOD makeSound. END CLASS Cat.

This example shows how different subclasses can override the same method with different implementations.

Method Signature Requirements

ElementRequirementExample
Method nameMust be identicalcalculateArea
Parameter typesMust match in order and typePIC 9(8)V99, PIC X(50)
Return typeMust be compatiblePIC 9(8)V99
OVERRIDE keywordMust be includedMETHOD-ID. name OVERRIDE

Practical Examples

Let's look at some real-world examples of how OVERRIDE is used in COBOL applications.

Bank Account 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
* Bank account inheritance with method overriding IDENTIFICATION DIVISION. PROGRAM-ID. BANK-ACCOUNTS. * Base account class CLASS-ID. BankAccount. DATA DIVISION. WORKING-STORAGE SECTION. 01 ACCOUNT-NUMBER PIC 9(10). 01 BALANCE PIC 9(10)V99. 01 INTEREST-RATE PIC 9(3)V99 VALUE 0.05. METHOD-ID. calculateInterest. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(10)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. COMPUTE RETURN-VALUE = BALANCE * INTEREST-RATE EXIT METHOD. END METHOD calculateInterest. METHOD-ID. displayInfo. DISPLAY "Account: " ACCOUNT-NUMBER DISPLAY "Balance: " BALANCE DISPLAY "Interest Rate: " INTEREST-RATE EXIT METHOD. END METHOD displayInfo. END CLASS BankAccount. * Savings account with override CLASS-ID. SavingsAccount INHERITS FROM BankAccount. DATA DIVISION. WORKING-STORAGE SECTION. 01 SAVINGS-RATE PIC 9(3)V99 VALUE 0.08. 01 MINIMUM-BALANCE PIC 9(10)V99 VALUE 100.00. METHOD-ID. calculateInterest OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(10)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Higher interest rate for savings COMPUTE RETURN-VALUE = BALANCE * SAVINGS-RATE EXIT METHOD. END METHOD calculateInterest. METHOD-ID. displayInfo OVERRIDE. * Call parent method first INVOKE SUPER "displayInfo" * Add savings-specific information DISPLAY "Account Type: Savings" DISPLAY "Minimum Balance: " MINIMUM-BALANCE EXIT METHOD. END METHOD displayInfo. END CLASS SavingsAccount. * Checking account with override CLASS-ID. CheckingAccount INHERITS FROM BankAccount. DATA DIVISION. WORKING-STORAGE SECTION. 01 CHECKING-RATE PIC 9(3)V99 VALUE 0.02. 01 MONTHLY-FEE PIC 9(5)V99 VALUE 10.00. METHOD-ID. calculateInterest OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(10)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Lower interest rate for checking COMPUTE RETURN-VALUE = BALANCE * CHECKING-RATE EXIT METHOD. END METHOD calculateInterest. METHOD-ID. displayInfo OVERRIDE. * Call parent method first INVOKE SUPER "displayInfo" * Add checking-specific information DISPLAY "Account Type: Checking" DISPLAY "Monthly Fee: " MONTHLY-FEE EXIT METHOD. END METHOD displayInfo. END CLASS CheckingAccount.

This example shows how different account types can override interest calculation and display methods.

Employee Management System

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
88
89
90
91
92
93
94
95
* Employee inheritance with salary calculation override IDENTIFICATION DIVISION. PROGRAM-ID. EMPLOYEE-MANAGER. * Base employee class CLASS-ID. Employee. DATA DIVISION. WORKING-STORAGE SECTION. 01 EMPLOYEE-ID PIC 9(6). 01 NAME PIC X(50). 01 BASE-SALARY PIC 9(8)V99. 01 HOURS-WORKED PIC 9(3). METHOD-ID. calculateSalary. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(8)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Basic salary calculation COMPUTE RETURN-VALUE = BASE-SALARY EXIT METHOD. END METHOD calculateSalary. METHOD-ID. displayInfo. DISPLAY "Employee ID: " EMPLOYEE-ID DISPLAY "Name: " NAME DISPLAY "Base Salary: " BASE-SALARY EXIT METHOD. END METHOD displayInfo. END CLASS Employee. * Manager subclass with override CLASS-ID. Manager INHERITS FROM Employee. DATA DIVISION. WORKING-STORAGE SECTION. 01 BONUS-RATE PIC 9(3)V99 VALUE 0.15. 01 TEAM-SIZE PIC 9(3). METHOD-ID. calculateSalary OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(8)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Manager gets base salary plus bonus COMPUTE RETURN-VALUE = BASE-SALARY + (BASE-SALARY * BONUS-RATE) EXIT METHOD. END METHOD calculateSalary. METHOD-ID. displayInfo OVERRIDE. * Call parent method first INVOKE SUPER "displayInfo" * Add manager-specific information DISPLAY "Position: Manager" DISPLAY "Team Size: " TEAM-SIZE DISPLAY "Bonus Rate: " BONUS-RATE EXIT METHOD. END METHOD displayInfo. END CLASS Manager. * Part-time employee subclass CLASS-ID. PartTimeEmployee INHERITS FROM Employee. DATA DIVISION. WORKING-STORAGE SECTION. 01 HOURLY-RATE PIC 9(5)V99. METHOD-ID. calculateSalary OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(8)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Part-time salary based on hours worked COMPUTE RETURN-VALUE = HOURS-WORKED * HOURLY-RATE EXIT METHOD. END METHOD calculateSalary. METHOD-ID. displayInfo OVERRIDE. * Call parent method first INVOKE SUPER "displayInfo" * Add part-time specific information DISPLAY "Position: Part-Time" DISPLAY "Hours Worked: " HOURS-WORKED DISPLAY "Hourly Rate: " HOURLY-RATE EXIT METHOD. END METHOD displayInfo. END CLASS PartTimeEmployee.

This example demonstrates salary calculation overrides for different employee types.

Advanced OVERRIDE Features

OVERRIDE supports advanced features for complex inheritance scenarios.

Calling Parent Method (SUPER)

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
* Calling parent method from override CLASS-ID. EnhancedAccount INHERITS FROM BankAccount. METHOD-ID. displayInfo OVERRIDE. * Call parent method first to show basic info INVOKE SUPER "displayInfo" * Add enhanced information DISPLAY "Enhanced Features:" DISPLAY "- Online Banking" DISPLAY "- Mobile App" DISPLAY "- 24/7 Support" EXIT METHOD. END METHOD displayInfo. METHOD-ID. calculateInterest OVERRIDE. DATA DIVISION. LINKAGE SECTION. 01 RETURN-VALUE PIC 9(10)V99. PROCEDURE DIVISION RETURNING RETURN-VALUE. * Get base interest from parent INVOKE SUPER "calculateInterest" RETURNING RETURN-VALUE * Add bonus for enhanced account COMPUTE RETURN-VALUE = RETURN-VALUE + (BALANCE * 0.01) EXIT METHOD. END METHOD calculateInterest. END CLASS EnhancedAccount.

You can call the parent method using INVOKE SUPER to extend rather than replace functionality.

Multiple Level 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
* Multiple inheritance levels with overrides CLASS-ID. GrandParent. METHOD-ID. displayInfo. DISPLAY "GrandParent Information" EXIT METHOD. END METHOD displayInfo. END CLASS GrandParent. CLASS-ID. Parent INHERITS FROM GrandParent. METHOD-ID. displayInfo OVERRIDE. * Call grandparent method INVOKE SUPER "displayInfo" DISPLAY "Parent Information" EXIT METHOD. END METHOD displayInfo. END CLASS Parent. CLASS-ID. Child INHERITS FROM Parent. METHOD-ID. displayInfo OVERRIDE. * Call parent method (which calls grandparent) INVOKE SUPER "displayInfo" DISPLAY "Child Information" EXIT METHOD. END METHOD displayInfo. END CLASS Child.

Method overriding can occur at multiple levels in an inheritance hierarchy.

Best Practices and Tips

Following these best practices will help you use OVERRIDE effectively in your COBOL applications.

OVERRIDE Best Practices

  • Always use OVERRIDE keyword - Makes intentions clear and catches signature changes
  • Maintain Liskov Substitution Principle - Ensure overridden methods maintain the same contract
  • Document behavioral changes - Document how overridden behavior differs from parent
  • Consider using SUPER - Use INVOKE SUPER when appropriate to extend functionality
  • Test polymorphic behavior - Test with different object types to ensure correct method resolution
  • Keep signatures consistent - Ensure method signatures match exactly with parent
  • Use meaningful method names - Choose method names that clearly indicate their purpose
  • Handle exceptions properly - Ensure overridden methods handle exceptions appropriately

Common Mistakes to Avoid

MistakeProblemSolution
Not using OVERRIDE keywordCompiler may not catch signature mismatchesAlways include OVERRIDE in METHOD-ID clause
Changing method signatureBreaks inheritance contractKeep exact same signature as parent method
Violating Liskov PrincipleSubclasses can't be used as parent typesMaintain same behavioral contract
Not calling SUPER when neededLose parent functionalityUse INVOKE SUPER to extend parent behavior
Overriding too many methodsComplex inheritance hierarchiesOnly override methods that need customization

OVERRIDE Quick Reference

ActionSyntaxExample
Basic overrideMETHOD-ID. name OVERRIDEMETHOD-ID. calculateArea OVERRIDE
Override with parametersMETHOD-ID. name OVERRIDE
PROCEDURE DIVISION USING params
METHOD-ID. processData OVERRIDE
PROCEDURE DIVISION USING INPUT-DATA
Call parent methodINVOKE SUPER "methodName"INVOKE SUPER "processData"
Inheritance declarationCLASS-ID. name INHERITS FROM parentCLASS-ID. Rectangle INHERITS FROM Shape
Polymorphic usageINVOKE obj "methodName"INVOKE SHAPE-OBJECT "calculateArea"
Check object typeIF obj IS INSTANCE OF classIF SHAPE-OBJECT IS INSTANCE OF Rectangle

Test Your Knowledge

1. What is the primary purpose of OVERRIDE in COBOL?

  • To create new methods
  • To override inherited methods in subclasses
  • To define data structures
  • To control program flow

2. Which COBOL concept does OVERRIDE support?

  • Data validation
  • Polymorphism
  • File I/O operations
  • Arithmetic calculations

3. Where is the OVERRIDE keyword placed in a method definition?

  • At the end of the method
  • In the METHOD-ID clause
  • In the PROCEDURE DIVISION
  • In the DATA DIVISION

4. What must match exactly when overriding a method?

  • Only the method name
  • Method name, parameters, and return type
  • Only the parameters
  • Only the return type

5. How do you call the parent method from an overridden method?

  • Using CALL PARENT
  • Using INVOKE SUPER
  • Using PERFORM PARENT
  • Using GO TO PARENT

Frequently Asked Questions