Wednesday, January 20, 2010

Dynamic Method Dispatch













Dynamic Method Dispatch

Inheritance with type hierarchies leads to dynamic method dispatch. Dynamic method dispatch is the method by which Oracle implements dynamic polymorphism. Polymorphism is ability to use multiple methods with the same name with different data. There are two types of polymorphism: static and dynamic. Static polymorphism refers to choosing the appropriate method based on the data at compile time and ideally is implemented using method overloading. Dynamic polymorphism refers to selecting the appropriate method to be executed depending on the data at runtime. In PL/SQL 9i this is implemented using dynamic method dispatch. In other words, dynamic method dispatch is the ability to determine at runtime and execute the most specific method in the object hierarchy that corresponds to the instance of the object type that invokes the method.


As evident from the previous section, you can override a method of an object type in the subtypes in a type hierarchy. In the Book hierarchy described earlier, the display_info method is first specified in the supertype book_type and overridden in each of the subtypes literature_type, fiction_type, novel_type, and mystery_type. This resulted in multiple implementations of display_info depending on the subtype. Now, when you use the type hierarchy, objects are defined based on the supertypes and subtypes. Then member methods are invoked based on the type of object defined. The method call is dispatched to the corresponding type's implementation and then executed.


How does PL/SQL determine which method implementation to use on the particular object instance? As an example, in the case of the lit_classic and classic_book objects defined earlier, how does PL/SQL know what method implementation of the display_info method is to be used? It does this by using dynamic method dispatch. PL/SQL uses that method implementation in the type hierarchy that is "nearest" to the type of object on which the method is invoked. It does this by checking, starting with the current type and navigating through the hierarchy to its supertypes, if any. This is done at runtime depending on the object instance on which the particular method is invoked. Here's an example:




declare
lit_classic literature_type;
classic_book mystery_type;
begin
lit_classic := literature_type('DREAMS UNLIMITED','Bulusu Lakshman',
0112224444,'Books International', 'Y',
'Fiction','Booker Prize');
lit_classic.display_info;
classic_book := mystery_type('DREAMS UNLIMITED','Bulusu Lakshman',
0112224444,'Books International', 'Y','Fiction',
'Booker Prize','Scientific','Y','Medical Related');
classic_book.display_info;
end;
/


Here's the output of this program:








The following points are worth noting:




  • Two different book objects are defined using the subtypes literature_type and mystery_type. Each of these objects is instantiated and its attributes populated using the appropriate constructor for the corresponding object type.





  • The display_info method is invoked on each object instance. The method name is the same for both the lit_classic and classic_book objects. For the lit_classic object, this displays the attribute information for its type as well as its supertype (i.e., book_type). For the classic_book object, this works out from the root of the type hierarchy and displays all information from all of its supertypes as well as its own specific attributes.




  • Compiling the types in the type hierarchy resolves the display_info method in each of the types and the code compiles successfully. But during execution of the previous PL/SQL block, dynamic method dispatch comes into play. The PL/SQL runtime engine does the following:




    • For the method call lit_classic.display_info, it knows the type of the object instance as literature_type. It checks whether this method is defined in literature_type. It is, and so it executes that implementation of this method.




    • For the method call classic_book.display_info, it recognizes the object instance as of type mystery_type and thus executes the implementation of display_info that's specific to the mystery_type object type.










Tip�

If a method is defined in one of the supertypes and not defined in a subtype, but the subtype calls this method, PL/SQL still executes the implementation of this method that's "nearest" to the supertype implementation. For example, consider a method called display_basic_lit_info in literature_type that isn't defined in any of its subtypes. If the classic_book object calls the display_basic_lit_info method and then the display_info method, the implementation of the display_basic_lit_info method is the one found in literature_type, but the implementation of the display_info method is the one given in mystery_type. This holds true for supertype methods calling methods that are overridden in the subtypes.












No comments: