Overriding an instance method

An instance method that is defined in a subclass is said to override an inherited instance method that would otherwise be accessible in the subclass if the two methods have the same signature.

About this task

To override a superclass instance method m1 in a COBOL subclass, define an instance method m1 in the subclass that has the same name and whose PROCEDURE DIVISION USING phrase (if any) has the same number and type of formal parameters as the superclass method has. (If the superclass method is implemented in Java™, you must code formal parameters that are interoperable with the data types of the corresponding Java parameters.) When a client invokes m1 on an instance of the subclass, the subclass method rather than the superclass method is invoked.

For example, the Account class defines a method debit whose LINKAGE SECTION and PROCEDURE DIVISION header look like this:


Linkage section.
01 inDebit    pic S9(9) binary.
Procedure Division using by value inDebit.

If you define a CheckingAccount subclass and want it to have a debit method that overrides the debit method defined in the Account superclass, define the subclass method with exactly one input parameter also specified as pic S9(9) binary. If a client invokes debit using an object reference to a CheckingAccount instance, the CheckingAccount debit method (rather than the debit method in the Account superclass) is invoked.

The presence or absence of a method return value and the data type of the return value used in the PROCEDURE DIVISION RETURNING phrase (if any) must be identical in the subclass instance method and the overridden superclass instance method.

An instance method must not override a factory method in a COBOL superclass nor a static method in a Java superclass.

Example: defining a method