Hyderabad Jobs Book Website FREE PowerBuilder Training I Love Hyderabad Hyderabad Colleges
Home Business Emails Hyderabad Classifieds Contact Us
7 Wonders of Hyderabad Web Hosting Yellow Pages Our Network

 
Webpowerbuilder.hyderabad-colleges.com

Advanced PowerBuilder

HomePrevious Lesson: Course 2:: Session 12 :: Page 100
Next Lesson: Polymorphism - PB Implementation

Polymorphism

In simple words, Polymorphism is the ability of a function to behave differently, depending on the context in which it is called.

Consider the example of calculating the salary of an employee. An employee can be a worker, a sales person, a manager, etc.. The calculation for each employee might be different. To implement this, you need to use either CASE or if.. else if., to call different versions of ComputePay() function. Using the CASE statements becomes a nightmare when modifying the system, since, each CASE statement needs to be updated. Polymorphism gives you the flexibility of avoiding the CASE and if.. else if statements. Here, at run-time, the compiler takes care of calling the appropriate function depending on the context in which the function was called.

Polymorphism can be implemented in two ways:
Overloading
Overriding

Overloading

A function is overloaded, when it is defined more than once, at the same object level, 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:
The number of arguments.
Same number of arguments but, different datatypes.

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 are 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. 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.

Overriding

For this, you need to use inheritance. In function overriding, the function name and arguments are typically same ( though they can be different ), but the code inside the function is different. For example, you can define a 'Save' function in the 'data-entry-form' object at the higher level and can have different code in each of the derived object, by defining the same name with different code. Saving a master form needs to update only the master file, and saving a transaction form needs to update master file as well as transaction file. In cases like this, function overriding can be used.
HomePrevious Lesson: Course 2:: Session 12 :: Page 100
Next Lesson: Polymorphism - PB Implementation

Copyright © 1996 - 2006 HamaraShehar.com Pvt. Ltd. All Rights Reserved.
Domain Registration, Website Design, Website Hosting by HamaraShehar.com