PREV NEXT
Java Dynamic Method Dispatch:
A superclass reference can refer to a subclass object. Whenever an overridden method is called through that superclass reference, then the call to that overridden method is resolved at run time based on the object being referred by the superclass reference.
Example program for Java Dynamic Method Dispatch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
Class A { Void display() { System.out.println(“A”); } } Class B extends A { Void display() { System.out.println(“B”); } } Class C extends A { Void display() { System.out.println(“C”); } } Class Test { public static void main(String args[]) { A a; a = new B(); a.display(); a = new C(); a.display(); } } |
Output:
B C |