Overloading an instance method

Two methods that are supported in a class (whether defined in the class or inherited from a superclass) are said to be overloaded if they have the same name but different signatures.

You overload methods when you want to enable clients to invoke different versions of a method, for example, to initialize data using different sets of parameters.

To overload a method, define a method whose PROCEDURE DIVISION USING phrase (if any) has a different number or type of formal parameters than an identically named method that is supported in the same class. For example, the Account class defines an instance method init that has exactly one formal parameter. The LINKAGE SECTION and PROCEDURE DIVISION header of the init method look like this:


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

Clients invoke this method to initialize an Account instance with a given account number (and a default account balance of zero) by passing exactly one argument that matches the data type of inAccountNumber.

But the Account class could define, for example, a second instance method init that has an additional formal parameter that allows the opening account balance to also be specified. The LINKAGE SECTION and PROCEDURE DIVISION header of this init method could look like this:


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

Clients could invoke either init method by passing arguments that match the signature of the required method.

The presence or absence of a method return value does not have to be consistent in overloaded methods, and the data type of the return value given in the PROCEDURE DIVISION RETURNING phrase (if any) does not have to be identical in overloaded methods.

You can overload factory methods in exactly the same way that you overload instance methods.

The rules for overloaded method definition and resolution of overloaded method invocations are based on the corresponding rules for Java™.