|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
POWERBUILDER LINKS YOU MUST CLICK ON PB & Java
PB And Java
By: Atul Kane
Digg This!
Most PowerBuilder developers are aware of object-oriented concepts like inheritance, encapsulation, and polymorphism. A lot has been said about them, and I don't want to reinvent the wheel by discussing what they are and how they're used in PB applications. The main purpose of this article is to explain one of the lesser known concepts of object-oriented programming, namely, upcasting. Later I'll give an example of method and variable overriding in conjunction with upcasting in PowerBuilder and how it differs in Java. I came across upcasting when I was preparing for Sun certification for Java. When I learned more about it, I realized that it's something common to PowerBuilder, too. In fact, I had used it before, but I didn't know the term. I then asked a few colleagues if they knew about it: these longtime PowerBuilder developer friends had no idea what was I talking about. That's when I decided to write an article about it.
What Is Upcasting ? int li_rowcount The return type of 'RowCount()' function is long. However, the above assignment is perfectly valid even though the variable 'li_rowcount' is declared 'int' (of course, there will be problems in the runtime if the 'Rowcount()' returns a value greater than the maximum 'Int' value). This is possible because of implicit casting supported by PowerBuilder Several low- and mid-level languages support explicit casting. Once you understand casting, upcasting should be obvious if you visualize the inheritance tree below. In this example the user object (or class) 'n_cst_son' is inherited from the user object (or class) from 'n_cst_father'. By virtue of inheritance, 'n_cst_son' class gets all the functions (in OO terminology, called as methods) and properties of the parent class. But that's not all. Inheritance also defines a 'relation' between child class and parent class. This relation is best described by saying: 'The child class is of type ÔParent' class.'
For example, in the script of your window, the following is perfectly legal:
n_cst_father lnv_father Here the object of child class is created and assigned to the object of type parent class. This is possible because of upcasting by virtue of inheritance. Examples of Upcasting At this point some of you may be wondering why, how, and where you'd use upcasting in PowerBuilder. Well, you've already used it if you ever passed a DataStore or nonvisual user object through Message object's PowerObjectParm. As you may recall, it's a common practice in PowerBuilder to pass structures and Data-Stores through a Message object. Since all PB objects are inherited from PowerObject, any PB object can be passed. Upcasting is indispensable if you're writing any framework or even a generic program. Consider this function, which returns the Filter string for both DataWindows and DataStores:
//parameter a_dw_or_ds PowerObject Listing 1 contains the complete code for the upcasting window.
Overriding Methods and
Variables in PB and Java I created two NVOs shown earlier: 'n_cst_son' inherited from 'n_cst_ father'. Each object has a defined one-instance variable for salary and both have a function with an identical signature that returns the age. In essence, the child class has overridden the variable salary as well as the function that returns the age.
On the command button of a window I coded the following
n_cst_father lnv_father
Now the question is, what would be the value of lnv.father.i_salary? Of lnv_father.uf_getage()? Similar code in Java reveals that Java never forgets the type of the object when looking for instance variables (see Figure 2 and Listing 3).
Summary
PBDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING POWERBUILDER / SYBASE NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||