|
July 1, 2002 Calling a Polymorphic Utility Tips: July 2002
Yehuda Shiran, Ph.D.
|
|
Let's look at a complete example that uses polymorphism. The main program section below creates four variables. The variable h1 is created by the CopyInt() constructor. The variable h2 is created by copying h1 with the polymorphic utility CopyMe(). The variable h3 is created by the CopyDouble() constructor, and is copied to h4 with the polymorphic utility CopyMe(). The utility CopyMe() is polymorphic because it operates on both integer and double values. The programmer who calls CopyMe() does not care how it is implemented. It is hidden from him. He or she can call CopyMe() with any object type. We print all variables at the end. Here is the code listing:
And here is the Command Prompt window, showing the code again (interfacepoly.js), the jsc compiler's log messages, and the output of interfacepoly.exe:
People who read this tip also read these tips: Look for similar tips by subject: |