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.
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.
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!
To override a method in COBOL, you must:
OVERRIDE keyword in the METHOD-ID clause12345678910111213141516171819*> 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.
Let's look at a practical example showing how different account types override the interest calculation method:
1234567891011121314151617181920212223242526272829303132333435363738IDENTIFICATION 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.
1234567891011121314151617181920212223242526272829303132333435363738394041IDENTIFICATION 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.
1234567891011121314151617181920212223242526272829303132333435363738394041IDENTIFICATION 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.
Sometimes you want to extend the parent method's functionality rather than completely replace it. You can call the parent method using INVOKE SUPER:
1234567891011121314151617181920212223242526272829CLASS-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.
When overriding a method, the signature must match exactly:
| Element | Requirement | Example |
|---|---|---|
| Method name | Must be identical | calculateInterest |
| Parameter types | Must match in order and type | PIC 9(8)V99, PIC X(50) |
| Return type | Must be compatible | PIC 9(8)V99 |
| OVERRIDE keyword | Must be included | METHOD-ID. name OVERRIDE |
Method overriding enables polymorphism. Here's how it works:
1234567891011121314151617181920212223242526272829IDENTIFICATION 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.
Forgetting the OVERRIDE keyword will cause compilation errors. Always include it when overriding methods.
Changing parameters or return type breaks the inheritance contract. The signature must match exactly.
Overridden methods should maintain the same behavioral expectations. Don't change the fundamental contract.
If you want to extend parent functionality, use INVOKE SUPER. Completely replacing may lose important behavior.
1. What is method overriding in COBOL?
2. What keyword must be used when overriding a method in COBOL?
3. What must match exactly when overriding a method?
4. How do you call the parent class method from an overridden method?
5. What is polymorphism in the context of method overriding?
6. Can you override a method without using the OVERRIDE keyword?
7. What is the Liskov Substitution Principle in relation to method overriding?
8. Can a subclass override multiple methods from its parent class?