Although the use cases for overloading methods and constructors are different, the requirements for defining an overloaded method or constructor are almost the same. To define an overloaded method or constructor, you must do one of the following:
Define a different number of parameters than any of the others.
Ensure that at least one common parameter among them has a different mode (INPUT, OUTPUT or INPUT-OUTOUT) from any of the others.
Ensure that at least one corresponding parameter among them has a different data type definition from any of the others. In addition to basic differences in data type, such as between different primitive types, different class or interface types, different temp-tables, and different ProDataSets, differences in data type can take the following forms:
For a data type that can be defined as an array, the difference can be whether it is defined as an array or not, whether it is defined as a determinate or indeterminate array, and whether two determinate arrays have different extents.
To differentiate static temp-table (TABLE) or static ProDataSet (DATASET) parameters, parameters of each type must differ in their defined schema.
A dynamic temp-table (TABLE-HANDLE) or ProDataSet (DATASET-HANDLE) parameter differs from any static temp-table or ProDataSet parameter, respectively. However, you can have only one method or constructor that differs by specifying a TABLE-HANDLE rather than a TABLE parameter, or by specifying a DATASET-HANDLE rather than a DATASET parameter. In other words, because they have no schema associated with them at compile time, ABL cannot distinguish multiple TABLE-HANDLE parameters from each other or multiple DATASET-HANDLE parameters from each other.
Finally, for methods, because method scope (specified by the STATIC option on the METHOD statement) does not participate in method overloading, you cannot define an instance and static method in a class that have the same method name and signature.
The following example shows a fragment of the sample class, acme.myObjs.CustObj, including the definitions of two overloaded methods distinguished by the number of parameters (one compared to none):
USING acme.myObjs.*.
USING acme.myObjs.Common.*.
USING acme.myObjs.Interfaces.*.
CLASS acme.myObjs.CustObj INHERITS CommonObj
IMPLEMENTS IBusObj:
...
DEFINE PROTECTED TEMP-TABLE ttCustomer NO-UNDO
FIELD ...
...
/* First version of printObj prints a single copy of a report */
METHOD PUBLIC VOID printObj ( ):
OUTPUT TO PRINTER.
DISPLAY dtTimestamp.
FOR EACH ttCustomer:
DISPLAY ttCustomer.
END.
OUTPUT CLOSE.
PublishOutputGenerated("One copy of report sent to printer").
END METHOD.
/* Second version of printObj takes an integer parameter representing the
number of copies to print. */
METHOD PUBLIC VOID printObj (INPUT piCopies AS INTEGER):
DEFINE VARIABLE iCnt AS INTEGER NO-UNDO.
OUTPUT TO PRINTER.
IF piCopies <> 0 THEN
DO iCnt = 1 TO ABS(piCopies):
DISPLAY dtTimestamp.
FOR EACH ttCustomer:
DISPLAY ttCustomer.
END.
END.
OUTPUT CLOSE.
PublishOutputGenerated(STRING(piCopies)
+ " copies of report sent to printer").
END METHOD.
...
END CLASS.
The following example shows a fragment of the sample class, Main, including the definitions of two overloaded constructors distinguished by the number of parameters (one compared to none):
USING acme.myObjs.*.
USING acme.myObjs.Common.*.
USING acme.myObjs.Interfaces.*.
CLASS Main:
DEFINE PRIVATE VARIABLE rCustObj AS CLASS CustObj NO-UNDO.
...
DEFINE PRIVATE VARIABLE rHelperClass AS CLASS HelperClass NO-UNDO.
DEFINE PRIVATE VARIABLE outFile AS CHARACTER.
CONSTRUCTOR PUBLIC Main ( ):
ASSIGN
/* Create an instance of the HelperClass class */
rHelperClass = NEW HelperClass ( )
/* Create an instance of the CustObj class */
rCustObj = NEW CustObj ( )
cOutFile = "Customers.out".
/* Subscribe OutputGenerated event handler for CustObj */
rCustObj:OutputGenerated:Subscribe(OutputGenerated_CustObjHandler).
END CONSTRUCTOR.
/* Second constructor takes a character parameter representing an input
file of email addresses to instantiate a New England Customer object */
CONSTRUCTOR PUBLIC Main (INPUT EmailFile AS CHARACTER):
ASSIGN
/* Create an instance of the HelperClass class */
rHelperClass = NEW HelperClass( )
/* Create an instance of the NECustObj class */
rCustObj = NEW NECustObj (EmailFile)
cOutFile = "NECustomers.out".
/* Subscribe OutputGenerated event handler for NECustObj */
rCustObj:OutputGenerated:Subscribe(OutputGenerated_NECustObjHandler).
END CONSTRUCTOR.
...
END CLASS.
For more information on defining parameters for overloaded methods and constructors, see the Parameter definition syntax reference entry in OpenEdge Development: ABL Reference.