//  Data Structures with Java by John R. Hubbard
//  Copyright McGraw-Hill, 2001
//  Example 3.4 on page 57
//  Casting reference types

class X { public String toString() { return "I am an X."; } }
class Y extends X { public String toString() { return "I am a Y."; } }

public class Ex0304
{ public static void main(String[] args) 
  { Y y = new Y();
    System.out.println("y: " + y);
    X x = y;
    System.out.println("x: " + x);
    Y yy = (Y)x;       // casts x as type Y
    System.out.println("yy: " + yy);
    X xx = new X();
    System.out.println("xx: " + xx);
    yy = (Y)xx;      // RUN-TIME ERROR: xx cannot be cast as a Y
    System.out.println("yy: " + yy);
  }  
}
