Method Overloading
Note: The technique of overloading methods is considered obsolete. You can and should use alternative strategies to achieve the same means:
- Use different method names for similar methods with different parameters:
Class cMyClass is a cObject
Function Foo Returns String
End_Function
Function FooForVal String sValue Returns String
:
End_Function
Procedure CallFoo
String sVal
Get Foo to sVal
Get FooForVal "Name" to sVal
End_Procedure
End_Class
- Use Num_Arguments (Variable Parameter Lists):
Class cMyArray is an Array
Procedure Foo String sValue
If (Num_Arguments = 0) Begin
:
End
End_Function
Procedure CallFoo
Send Foo
Send Foo "Name"
End_Procedure
End_Class
Many object-oriented languages support alternate methods with different parameter lists. This is referred to as overloading. There are at least two types of overloading:
Overloading on Parameter Count
When overloading on parameter count, methods with different numbers of parameters are treated as if they are different methods (in fact, they are). When you call a method, the system will check the number of parameters passed and search for a method of this name with the same number of parameters. If found, the message is sent. If not found, an error occurs. This type of overloading is most appropriate for loosely typed languages like DataFlex.
Overloading on Parameter Count and Data Type
This method takes overloading one step further and looks at both parameter count and type when resolving a message. When you call a method, the system will check the number of parameters passed and the data type of each parameter, and search for a method of that name with the same number of parameters and data types. If found, the message is sent. If not found, an error occurs. This type of overloading is most appropriate for strongly typed languages such as Java.
DataFlex supports the ability to declare a method as being overloaded. It overloads on parameter count only. Methods are not overloaded by default.
Declaring Overloaded Methods
All overloaded methods must be explicitly declared as being overloaded. A method is defined as being overloaded by passing the token Overloaded in the method definition.
Class cMyArray is an Array
Function Foo Overloaded Returns String
:
End_Function
Function Foo Overloaded String sValue Returns String
:
End_Function
Procedure CallFoo
String sVal
Get Foo to sVal // this calls the first Foo
Get Foo "Name" to sVal // this calls the 2nd Foo
End_Procedure
End_Class
When methods are overloaded, the number of parameters determines which method gets called. Note that attempting to call an overloaded method with the wrong number of parameters will generate an error. For example:
// This will generate a compiler error
Get Foo "Name" "Name2" to sVal2
Overloaded methods with different numbers of parameters are treated as different methods. It is valid (and quite common) to call one overloaded method from within another one. This provides a way to support different numbers of parameters within a message. For example:
Class cMyArray is an Array
Procedure Foo Overloaded
// if no parameter is passed, provide a default
Send Foo "Default Name"
End_Procedure
Procedure Foo Overloaded String sVal
Showln "Value is " sVal
End_Procedure
Procedure CallFoo
Send Foo // will show "Default Name"
Send Foo "My Name" // will show "My Name"
End_Procedure
End_Class
Note that the overloading is based on the number of parameters. If two methods have the same number of parameters but different types, they are considered the same.
Class cMyArray is an Array
// this Foo will never get called
Procedure Foo Overloaded Integer iVal
End_Procedure
Procedure Foo Overloaded String sVal
End_Procedure
End_Class
DataFlex supports overloading of class methods (Functions and Procedures). Property names cannot be overloaded. Global functions and procedures, which are not really considered to be methods, cannot be overloaded.
An application may contain overloaded methods and regular (non-overloaded) methods. Overloaded and non-overloaded methods may not share the same name. The technique for resolving these types of methods is very different. The compiler and runtime must know what kind of message it is trying to resolve. If you attempt to create an overloaded method that has already been defined as non-overloaded, you will receive a compiler error. If you attempt to create a non-overloaded method that has already been defined as overloaded, you will receive a compiler error.
Class cMyArray is an Array
Procedure Foo
End_Procedure
// this will generate a compiler error
Procedure Foo Overloaded
End_Procedure
Procedure Goo Overloaded
End_Procedure
// this will generate a compiler error
Procedure Goo
End_Procedure
End_Class