COBOL Tutorial

COBOL Method Overriding

Method overriding is a fundamental concept in object-oriented COBOL that allows subclasses to provide their own implementation of methods inherited from parent classes. This enables polymorphism - the ability to use the same interface for different implementations, making your code more flexible and maintainable.

What is Method Overriding?

Method overriding occurs when a subclass defines a method with the same signature (name, parameters, and return type) as a method in its parent class. The subclass provides its own implementation, effectively replacing the parent's implementation for objects of that subclass type.

Key Concepts

  • Inheritance: A subclass inherits methods from its parent class
  • Override: The subclass provides a new implementation of an inherited method
  • Polymorphism: The same method call can execute different code depending on the actual object type
  • Runtime Resolution: The actual method executed is determined at runtime based on the object's actual type

Understanding Method Overriding (Like You're 5 Years Old)

Imagine you have a parent who knows how to "cook dinner" - they have a basic recipe. Now imagine you have children who also know how to "cook dinner," but each child has their own special way of doing it - one adds extra spices, another uses different ingredients, but they all call it "cook dinner."

In COBOL, the parent class has a method called "cookDinner". When a child class (subclass) inherits from the parent, it gets that method. But if the child wants to cook dinner their own way, they can "override" the method - they keep the same name "cookDinner" but write their own recipe (code). When someone asks any of them to "cook dinner," each one does it their own way!

Basic Syntax

To override a method in COBOL, you must:

  • Define the method in a subclass that inherits from the parent class
  • Use the OVERRIDE keyword in the METHOD-ID clause
  • Match the method signature exactly (name, parameters, return type)
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Parent class with a method CLASS-ID. Animal. METHOD-ID. makeSound. DISPLAY "Some animal sound" EXIT METHOD. END METHOD makeSound. END CLASS Animal. *> Subclass overriding the method CLASS-ID. Dog INHERITS FROM Animal. METHOD-ID. makeSound OVERRIDE. DISPLAY "Woof! Woof!" EXIT METHOD. END METHOD makeSound. END CLASS Dog.

Complete Example: Bank Account Hierarchy

Let's look at a practical example showing how different account types override the interest calculation method:

Base Account 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
IDENTIFICATION DIVISION. CLASS-ID. BankAccount AS "BankAccount". ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS java-lang-Object. DATA DIVISION. WORKING-STORAGE SECTION. OBJECT. 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. PROCEDURE DIVISION. 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 OBJECT. END CLASS BankAccount.

Savings Account Subclass (With 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
34
35
36
37
38
39
40
41
IDENTIFICATION DIVISION. CLASS-ID. SavingsAccount AS "SavingsAccount". ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS BankAccount CLASS java-lang-Object. DATA DIVISION. WORKING-STORAGE SECTION. OBJECT. 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. PROCEDURE DIVISION. 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 accounts 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 OBJECT. END CLASS SavingsAccount.

Checking Account Subclass (With 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
34
35
36
37
38
39
40
41
IDENTIFICATION DIVISION. CLASS-ID. CheckingAccount AS "CheckingAccount". ENVIRONMENT DIVISION. CONFIGURATION SECTION. REPOSITORY. CLASS BankAccount CLASS java-lang-Object. DATA DIVISION. WORKING-STORAGE SECTION. OBJECT. 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. PROCEDURE DIVISION. 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 accounts 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 OBJECT. END CLASS CheckingAccount.

Using SUPER to Call Parent Methods

Sometimes you want to extend the parent method's functionality rather than completely replace it. You can call the parent method using INVOKE 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
CLASS-ID. EnhancedAccount INHERITS FROM BankAccount. METHOD-ID. displayInfo OVERRIDE. *> Call parent method first to show basic info INVOKE SUPER "displayInfo" *> Add enhanced account 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.

Method Signature Requirements

When overriding a method, the signature must match exactly:

ElementRequirementExample
Method nameMust be identicalcalculateInterest
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

Polymorphism in Action

Method overriding enables polymorphism. Here's how it works:

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
IDENTIFICATION DIVISION. PROGRAM-ID. PolymorphismExample. DATA DIVISION. WORKING-STORAGE SECTION. 01 account-ref USAGE IS OBJECT REFERENCE BankAccount. 01 savings-ref USAGE IS OBJECT REFERENCE SavingsAccount. 01 checking-ref USAGE IS OBJECT REFERENCE CheckingAccount. 01 interest-amt PIC 9(10)V99. PROCEDURE DIVISION. MAIN-LOGIC. *> Create a savings account INVOKE SavingsAccount "new" RETURNING savings-ref SET account-ref TO savings-ref *> Call calculateInterest - will use SavingsAccount version INVOKE account-ref "calculateInterest" RETURNING interest-amt DISPLAY "Savings interest: " interest-amt *> Create a checking account INVOKE CheckingAccount "new" RETURNING checking-ref SET account-ref TO checking-ref *> Call calculateInterest - will use CheckingAccount version INVOKE account-ref "calculateInterest" RETURNING interest-amt DISPLAY "Checking interest: " interest-amt STOP RUN.

Even though account-ref is declared as a BankAccount reference, when it points to a SavingsAccount object, calling calculateInterest executes the SavingsAccount version. This is polymorphism - the same method call produces different results based on the actual object type.

Step-by-Step Guide: Creating an Override

Best Practices

  • Always use OVERRIDE keyword: Makes your intentions clear and helps the compiler verify signatures
  • Maintain Liskov Substitution Principle: Overridden methods should maintain the same behavioral contract as the parent
  • Document behavioral changes: Add comments explaining how your override differs from the parent
  • Consider using SUPER: Use INVOKE SUPER when appropriate to extend rather than replace functionality
  • Test polymorphic behavior: Test with different object types to ensure correct method resolution
  • Keep signatures consistent: Ensure method signatures match exactly with parent methods
  • Use meaningful method names: Choose names that clearly indicate their purpose

Common Mistakes to Avoid

Not Using OVERRIDE

Forgetting the OVERRIDE keyword will cause compilation errors. Always include it when overriding methods.

Changing Method Signature

Changing parameters or return type breaks the inheritance contract. The signature must match exactly.

Violating Liskov Principle

Overridden methods should maintain the same behavioral expectations. Don't change the fundamental contract.

Not Calling SUPER When Needed

If you want to extend parent functionality, use INVOKE SUPER. Completely replacing may lose important behavior.

Key Takeaways

  • Method overriding allows subclasses to provide custom implementations of inherited methods
  • The OVERRIDE keyword is required and must be included in the METHOD-ID clause
  • Method signatures (name, parameters, return type) must match exactly with the parent method
  • Use INVOKE SUPER to call the parent method when extending functionality
  • Method overriding enables polymorphism - the same method call produces different results based on object type
  • Overridden methods should maintain the Liskov Substitution Principle - subclasses should be usable wherever parent classes are expected
  • Runtime resolution determines which method executes based on the actual object type, not the reference type

Test Your Knowledge

1. What is method overriding in COBOL?

  • Creating a new method with a different name
  • Defining a method in a subclass with the same signature as a method in the superclass
  • Deleting a method from a parent class
  • Renaming a method in a subclass

2. What keyword must be used when overriding a method in COBOL?

  • REPLACE
  • OVERRIDE
  • REDEFINE
  • OVERRIDING

3. 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

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

  • CALL PARENT "methodName"
  • INVOKE SUPER "methodName"
  • PERFORM PARENT-METHOD
  • EXEC PARENT methodName

5. What is polymorphism in the context of method overriding?

  • Having multiple methods with the same name but different parameters
  • The ability to use a subclass object where a superclass is expected, with the correct method being called at runtime
  • Creating multiple classes with the same name
  • Sharing data between classes

6. Can you override a method without using the OVERRIDE keyword?

  • Yes, it will work the same way
  • No, the compiler will generate an error
  • Yes, but it is not recommended and may cause issues
  • Only in certain COBOL versions

7. What is the Liskov Substitution Principle in relation to method overriding?

  • Subclasses must be able to replace their parent classes without breaking functionality
  • Methods must be overridden in alphabetical order
  • All methods must be overridden in subclasses
  • Subclasses cannot override parent methods

8. Can a subclass override multiple methods from its parent class?

  • No, only one method can be overridden
  • Yes, a subclass can override any number of inherited methods
  • Only if the parent class has fewer than 5 methods
  • Only factory methods can be overridden