Polymorphism in COBOL refers to the object-oriented (OO) feature where the same method name can behave differently depending on the actual type of object at runtime. This is achieved through inheritance, method overriding, and interfaces. Not all COBOL compilers support OO; Enterprise COBOL for z/OS and many others do. This page explains how polymorphism works in OO COBOL, how to override methods, how to invoke methods and superclass implementations, and how interfaces contribute to polymorphic design.
Think of "make a sound." A dog says "woof," a cat says "meow," and a bird might "tweet." They all "make a sound," but each does it differently. In the program we just say "make a sound" and the right noise happens depending on whether we have a dog, cat, or bird. Polymorphism is that idea: one name (the method), many behaviors (each class does it its own way), and the computer picks the right one based on the actual object you have.
COBOL supports several forms of polymorphism. The most common in OO COBOL are class polymorphism (inheritance and overriding) and interface polymorphism (different classes implementing the same interface). Some compilers also support method overloading (parametric polymorphism) and ad-hoc techniques using REDEFINES or conditional logic.
| Type | Description | How it is done |
|---|---|---|
| Class (inheritance) polymorphism | Same method name in a class hierarchy; subclass overrides superclass method. Runtime type decides which implementation runs. | OVERRIDE in subclass; INVOKE on object reference. |
| Interface polymorphism | Different classes implement the same interface (same method names/signatures). Code can work with the interface type and any implementing class. | INTERFACE-ID; class implements interface; reference to interface type. |
| Parametric (overloading) | Same method name with different parameter lists. Compiler picks the right method by arguments. | Multiple methods with same name, different USING signatures. |
| Ad-hoc (REDEFINES / conditions) | One method or block handles different data or cases using REDEFINES or class tests. | REDEFINES, condition names, or IF/EVALUATE on type. |
COBOL uses single inheritance: a class has at most one direct superclass. A subclass inherits methods and (through defined attributes) data from the superclass. When a subclass provides a new implementation of an inherited method with the same name and signature, that is method overriding. You must use the OVERRIDE keyword in the method definition so the compiler knows you are replacing the inherited behavior. The method name, the number and types of parameters (USING), and the return type (RETURNING) must match the superclass method exactly. At runtime, when you INVOKE that method on an object reference that actually refers to the subclass instance, the overridden implementation runs—that is polymorphic behavior.
1234567891011121314*> Superclass: REPORT-WRITER METHOD-ID. "writeReport" OVERRIDE. PROCEDURE DIVISION RETURNING report-status. ... END METHOD. *> Subclass: PDF-REPORT-WRITER inherits REPORT-WRITER METHOD-ID. "writeReport" OVERRIDE. PROCEDURE DIVISION RETURNING report-status. *> Subclass-specific PDF logic INVOKE SUPER "writeReport" RETURNING report-status *> then add PDF-specific steps ... END METHOD.
Here the subclass overrides "writeReport" and still calls the superclass version with INVOKE SUPER "writeReport" RETURNING report-status before or after its own logic. The caller only says INVOKE myWriter "writeReport" RETURNING status; whether myWriter is a base REPORT-WRITER or a PDF-REPORT-WRITER, the correct implementation runs.
INVOKE is how you call methods on objects (or on classes for factory/static methods). The method that actually runs is determined at runtime from the object's actual class, which is why INVOKE is the main vehicle for polymorphism. You can pass arguments with USING (BY VALUE, BY REFERENCE, BY CONTENT as applicable) and receive a result with RETURNING. ON EXCEPTION and NOT ON EXCEPTION handle errors when the method is not found or raises a condition.
| Form | Meaning | Example |
|---|---|---|
| INVOKE object-ref "method-name" | Call an instance method on an object. The implementation is that of the object's actual class (polymorphism). | INVOKE myAccount "credit" USING BY VALUE 100. |
| INVOKE class-name "method-name" | Call a factory or static method on the class. No object instance needed. | INVOKE Account "createAccount" RETURNING myAccount. |
| INVOKE SELF "method-name" | Call a method on the current object (inside a method). Often used to call another method of the same object. | INVOKE SELF "validate". |
| INVOKE SUPER "method-name" | Call the superclass version of the method, bypassing the override in the current class. | INVOKE SUPER "display" USING WS-MSG. |
The method name can be a literal (e.g. "credit") or an alphanumeric identifier whose value at runtime is the method name. When using an object reference, the compiler and runtime resolve the method in the object's actual class first, then up the inheritance chain if necessary. If the method is not found, a run-time exception can occur unless you handle it with ON EXCEPTION.
When a subclass overrides a method, the subclass implementation runs whenever that method is invoked on an instance of the subclass. Sometimes the subclass needs to call the superclass's version—for example to reuse its logic and then add more. INVOKE SUPER "method-name" USING ... RETURNING ... does that. It runs the implementation from the immediate superclass, not the current class. You typically use it inside the overridden method. Only one level of SUPER is defined; to go further up the chain you would need the superclass to expose another method or use a different design.
An interface defines a set of method prototypes (names and signatures) that a class can implement. Different classes can implement the same interface, so you can write code that works with "anything that implements this interface" without caring which concrete class it is. That is interface polymorphism. You might have an interface PRINTABLE with a method "print". Both REPORT-WRITER and FORM-WRITER implement PRINTABLE. A variable declared as OBJECT REFERENCE PRINTABLE can hold any object that implements PRINTABLE; when you INVOKE that reference "print", the actual class's implementation runs. So the same INVOKE works for multiple implementing classes.
Interfaces do not contain implementation; they only define the contract. A class can implement multiple interfaces (unlike inheritance, where you have only one superclass). This lets you mix polymorphic behavior along several dimensions (e.g. both PRINTABLE and SAVABLE) in one class.
When you INVOKE object-ref "method-name", the runtime resolves the method as follows: it looks at the actual class of the object (the type it was created as, not necessarily the declared type of the reference). It then looks for a method with that name in that class. If not found, it searches the superclass, then that class's superclass, and so on. The first matching method is used. So if you have a reference declared as OBJECT REFERENCE REPORT-WRITER but it actually points to a PDF-REPORT-WRITER instance, INVOKE that reference "writeReport" runs PDF-REPORT-WRITER's overridden method. That dynamic resolution is what makes polymorphism work.
COBOL does not support multiple inheritance: a class has one superclass. You can implement multiple interfaces to get multiple contracts. Not all COBOL compilers support OO; check your product for CLASS-ID, METHOD-ID, INVOKE, and OVERRIDE. Method overloading (same name, different parameter lists) is supported in some compilers; the compiler chooses the method by the arguments. Older procedural COBOL has no polymorphism; this applies to the OO COBOL subset.
1. Polymorphism in OO COBOL means:
2. To override an inherited method you must:
3. INVOKE SUPER is used to: