Advanced PowerBuilder

HomePrevious Lesson: Polymorphism
Next Lesson: Inclusional Polymorphism OR Overriding

Overloading

A function is overloaded when it is defined more than once either at the same object level or in its inheritance hierarchy with the same name.

When a function is defined more than once with the same name, the interface will be different in one of the following way:

For example, the MessageBox function. It displays a given message on the screen. You can call this function with two parameters:
MessageBox("Title", "Message to Display")

You can also call the same function with five parameters:
MessageBox("Title", "Message to Display", Icon, Buttons, DefaultValue)

Here, you are calling the same function, but the number of arguments is different.

In the first format, both parameters are of string data type. However, there is another format that accepts both String and Integer:
MessageBox( "Title", Error-Number )

You can implement function overloading in two ways. If the language allows you to define it at the same object level, you can do so. Otherwise, you need to use inheritance to implement function overloading. At the highest level, define the function and create a new class by deriving from the highest class and define the same function in the derived class with either different arguments or different data types, depending on the need. The later method comes with a price, if you want to implement more than two flavors of the same function, since, you need to derive one class for each format of the function. In the above example, you need to use at least three levels of inheritance.

Compiler resolves the references at run-time, depending on the context, i.e., the arguments supplied, argument datatypes, etc... For objects using inheritance, the compiler searches up the inheritance hierarchy to resolve an implied call to the function.
HomePrevious Lesson: Polymorphism
Next Lesson: Inclusional Polymorphism OR Overriding