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.
Imagine you're running a restaurant chain:
OVERRIDE lets each restaurant type customize how they cook while keeping the same "cook" interface.
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.
123456789101112131415161718192021222324252627282930313233* 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.
Element | Requirement | Example |
---|---|---|
Method name | Must be identical | calculateArea |
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 |
Let's look at some real-world examples of how OVERRIDE is used in COBOL applications.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091* 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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495* 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.
OVERRIDE supports advanced features for complex inheritance scenarios.
1234567891011121314151617181920212223242526272829303132* 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.
12345678910111213141516171819202122232425262728293031* 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.
Following these best practices will help you use OVERRIDE effectively in your COBOL applications.
Mistake | Problem | Solution |
---|---|---|
Not using OVERRIDE keyword | Compiler may not catch signature mismatches | Always include OVERRIDE in METHOD-ID clause |
Changing method signature | Breaks inheritance contract | Keep exact same signature as parent method |
Violating Liskov Principle | Subclasses can't be used as parent types | Maintain same behavioral contract |
Not calling SUPER when needed | Lose parent functionality | Use INVOKE SUPER to extend parent behavior |
Overriding too many methods | Complex inheritance hierarchies | Only override methods that need customization |
Action | Syntax | Example |
---|---|---|
Basic override | METHOD-ID. name OVERRIDE | METHOD-ID. calculateArea OVERRIDE |
Override with parameters | METHOD-ID. name OVERRIDE PROCEDURE DIVISION USING params | METHOD-ID. processData OVERRIDE PROCEDURE DIVISION USING INPUT-DATA |
Call parent method | INVOKE SUPER "methodName" | INVOKE SUPER "processData" |
Inheritance declaration | CLASS-ID. name INHERITS FROM parent | CLASS-ID. Rectangle INHERITS FROM Shape |
Polymorphic usage | INVOKE obj "methodName" | INVOKE SHAPE-OBJECT "calculateArea" |
Check object type | IF obj IS INSTANCE OF class | IF SHAPE-OBJECT IS INSTANCE OF Rectangle |
1. What is the primary purpose of OVERRIDE in COBOL?
2. Which COBOL concept does OVERRIDE support?
3. Where is the OVERRIDE keyword placed in a method definition?
4. What must match exactly when overriding a method?
5. How do you call the parent method from an overridden method?
Understanding inheritance concepts in object-oriented COBOL.
Understanding polymorphism in object-oriented COBOL.
Working with method overriding in COBOL.
Understanding object-oriented design principles.
Understanding the Liskov Substitution Principle.