Try OpenEdge Now
skip to main content
Object-oriented Programming
Object-oriented Programming and ABL : Overview of object-oriented programming : Method overloading
 

Method overloading

Method overloading allows a class to define multiple methods with the same name, but different signatures. That is, it allows you to define different methods that have the same name, but that respond to correspondingly different messages sent to an instance of the class.
The following figure shows an example of overloading where a ClassA defines two overloads of a MethodA( ).
Figure 5. Method overloading example
In this example, the two methods return similar data as an output parameter, but one of them takes an input parameter that allows the method to conditionally change the data before returning it. The only requirement for the overloading to work, different method signatures, is satisfied by the additional input parameter in one of methods.
There is no requirement that overloaded methods have any functional relationship to each other, and they can, in fact, each implement behavior that is totally unrelated to the others. However, method overloading provides a notational convenience that allows you to define similar (but different) methods that provide a related set of behaviors (indicated by the same method name), but where access to a given implementation of the behavior requires a different set of parameters.
For a practical example using a Shape super class, a method to calculate the area of a shape really requires a different signature, depending on the shape. So, for example, to calculate the area of a rectangle, you need two values, its length and width, but to calculate the area of a circle, you need only one value, its radius. Therefore, for Shape, you might define two overloads of an Area( ) method, one that takes two numeric parameters (parm1 and parm2) and one that takes a single numeric parameter (parm). Then, when calculating the area of a rectangle, you call the Area(parm1,parm2) overload of the method with a definition that calculates the area from a length and width. When calculating the area of a circle, you call the Area(parm) overload of the method with a definition that calculates the area from a radius.
Note that this example can also rely on the polymorphic overriding of every Area( ) method overload in each subclass. For example, in order for each subclass to respond when a particular Area( ) overload does not apply, the super class implementation of these methods can be defined to raise an error message indicating that the method does not apply to the given subclass. Invoking code can simply call each method overload, and respond accordingly. For more information on polymorphism, see Polymorphism.