5.5 Dynamic Polymorphism

 < Day Day Up > 



5.5 Dynamic Polymorphism

Up until now all the tables we have created have stored only Car objects or Person objects. Because both types of objects implement the Printable interface, what is to prevent storing both Car objects and Person objects in the same table? The answer is nothing prevents it, and in fact it is easily done, as shown in Exhibit 8 (Program5.3e). When running Exhibit 8 (Program5.3e), the reader will note that both Person and Car objects are properly stored in the table and accessed at runtime. What is interesting is that when the print method of the Printable object is called it references the print method in the correct object; a Person object calls the print method in the Person class, and the Car object calls the print method in the Car class. Because the program does not know until runtime what type of object will be stored at each location in the table, the JVM must be determining the object type and thus the correct method to call at runtime, which is referred to as dynamic polymorphism. The term polymorphism comes from the Greek terms poly ("many") and morphous ("forms" or "shapes"). In object-oriented programming (OOP), dynamic polymorphism means that the object used and hence the method called from a component can actually change as the algorithm is running.

Exhibit 8: Program5.3e: Implementing a PrintTable that Stores Both Person and Car Objects

start example

 public class Polymorphism {   public static void main(String args[]) {     PrintTable T1 = new PrintTable(10);       T1.add(new Person("Benoit"));       T1.add(new Car(340));       T1.add(new Person("Roget"));       T1.add(new Car(200));       T1.printAll();   } } 

end example

The implementation of dynamic polymorphism in Java is easy to explain, given the explanation in the previous chapter that all objects in Java have a runtime data type tag, and the runtime data type is kept in a tag associated with the actual object variable. When the program is running, the JVM simply looks at the data type tag and finds the actual class (not the interface) that this object represents. Because the class for the object implements the Printable interface, we know that it must have implemented the print method. The JVM finds the object's class and calls the print method associated with that object. The important point here is that the method that is called is determined when the program is running, not when it is compiled.

It should be noted that the interface really plays no part in determining which print method is actually called at runtime. It is simply a compile time check to make sure the object obeys the rules of the interface promise. The promise is actually fulfilled by an object that implements the print method; hence, the calling of the methods for an interface is dynamic at runtime.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net