Fedora 12: Constantine

Wednesday, April 9, 2008

Dynamic Method Dispatch - Java

Method overriding forms the basis for one of Java’s most powerful concepts: dynamic method dispatch. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. Dynamic method dispatch is important because this is how Java implements run-time polymorphism.

A superclass reference variable can refer to a subclass object. Java uses this fact to resolve calls to overridden methods at run time. Here is how. When an overridden method is called through a superclass reference, Java determines which version of that method to execute based upon the type of the object being referred to at the time the call occurs. Thus, this determination is made at run time. When different types of objects are referred to, different versions of an overridden method will be called. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Therefore, if a superclass contains a method that is overridden by a subclass, then when different types of objects are referred to through a superclass reference variable, different versions of the method are executed.

  1. // Dynamic Method Dispatch
  2. class A {
  3. void callme() {
  4. System.out.println("Inside A's callme method");
  5. }
  6. }
  7. class B extends A {
  8. // override callme()
  9. void callme() {
  10. System.out.println("Inside B's callme method");
  11. }
  12. }
  13. class C extends A {
  14. // override callme()
  15. void callme() {
  16. System.out.println("Inside C's callme method");
  17. }
  18. }
  19. class Dispatch {
  20. public static void main(String args[]) {
  21. A a = new A(); // object of type A
  22. B b = new B(); // object of type B
  23. C c = new C(); // object of type C
  24. A r; // obtain a reference of type A
  25. r = a; // r refers to an A object
  26. r.callme(); // calls A's version of callme
  27. r = b; // r refers to a B object
  28. r.callme(); // calls B's version of callme
  29. r = c; // r refers to a C object
  30. r.callme(); // calls C's version of callme
  31. }
  32. }
The output of the program would be :

Inside A’s callme method
Inside B’s callme method
Inside C’s callme method

This program creates one superclass called A and two subclasses of it, called B and C. Subclasses B and C override callme( ) declared in A. Inside the main( ) method, objects of type A, B, and C are declared. Also, a reference of type A, called r, is declared. The program then assigns a reference to each type of object to r and uses that reference to invoke callme( ). As the output shows, the version of callme( ) executed is determined by the type of object being referred to at the time of the call. Had it been determined by the type of the reference variable, r, you would see three calls to A’s callme( ) method.


No comments: