Try OpenEdge Now
skip to main content
Object-oriented Programming
Designing Objects: Inheritance, Polymorphism, and Delegation : Class hierarchies and inheritance : Overloading methods and constructors
 

Overloading methods and constructors

A class definition can provide multiple methods, including constructors, that have the same name but different signatures. The signatures defined for overloaded methods and constructors must be different from one another in at least one variation in the number, type, or mode of their parameters. However, note that a static method cannot overload an instance method with an identical signature, because the method scope (determined by use of the STATIC option in the METHOD statement) does not count in the overloading.
Overloading thus allows a class to be instantiated, and allows multiple methods of a class with the same name to be called, using different arguments. Regardless of the arguments, a constructor always instantiates a class of the same type as any other overloaded constructor defined for the class, and a method of a given name typically invokes behavior that is similar to the behavior of every other overloaded method of the same name. In fact, it does not matter what behavior each method overloading provides, as long as the compiler can disambiguate the signatures at both compile time and run time. Thus, for methods, overloading provides a convenience, allowing you to conserve method names over some set of behaviors. For classes, constructor overloading provides both the convenience and power of being able to instantiate the same type of object using different sets of initial data. Note, however, that only instance constructors support overloading, as a class can have only one static constructor.
A class definition can overload its own methods and constructors. As a subclass, it can also inherit the overloaded methods (except the constructors) of a super class. It can also overload methods inherited from a super class, and it can override any existing overloadings of the methods that it inherits.
Method overloading provides a mechanism that you can use to invoke different behaviors in a similar way, somewhat like polymorphism (overriding). However, method overloading is much less powerful than polymorphism, because although you call a method of the same name, you need to specify a different signature to identify the different behavior, as compared to calling an identical overridden method on a different subclass instance to provide the different behavior polymorphically. Thus, method overloading represents an economy of notation (provided by conserving method names) rather than an economy of programming (provided by calling a single method), as represented by overriding. For more information on overriding, see Overriding methods within a class hierarchy.
* Defining overloaded methods and constructors
* Invoking overloaded methods and constructors