The Java Program: Overload.java

  1 // Overload.java
  2 
  3 class Y {
  4     void callme() {
  5         System.out.println ("Y's callme method");
  6     }
  7 }
  8 
  9 class Z extends Y {
 10     void callme() {
 11         System.out.println ("Z's callme method");
 12     }
 13 }
 14 
 15 class Overload {
 16 
 17    static void f (Y y) { y.callme(); }
 18 
 19    static void g (Y y) { System.out.println ("g called with a 'Y'"); }
 20    static void g (Z z) { System.out.println ("g called with a 'Z'"); }
 21    
 22    public static void main (String args[]) {
 23       /*
 24         The method actually invoked depends on the
 25         static type ("Y" in this case) of the object ("a"),
 26         and not on the value of the object at runtime.
 27       */
 28 
 29       Y a = new Z ();
 30       f(a);     // Z's method called
 31       g(a);     // g for Y called
 32       g((Z) a); // g for Z called
 33     }
 34 }