// this is a demo of up-casting and down-casting in Java

public class Casting
{
  public static void main(String[] args)
  {
    Rect r = new Rect();       // create two objects, both are syblings of
    Circ c = new Circ();       // the class Shape 

    Shape s = r;               // upcasting is always legal
    Circ c2 = (Circ) s;        // downcasting should be used with care
                               // and can lead to run-time errors

    r = new Square();          // this is perfectly legal
//    Square q = (new Rect());   // compilation error!
    Square q = (Square) (new Rect()); // compiles but causes a runtime error
  }
}

class Square extends Rect
{
  private int size;
}
