Please use the buttons for tags, much quicker than writing them by hand, and more effective.
In the first case you are declaring your object as an Ab; there is no z field in Ab. When you have a new declaration as a Bc, then it is a class which does have a z field.
When you declare a variable of a particular type, the compiler can only find the members of the declared type. At runtime the versions of the actual type are used, which is polymorphism and works for overridden members (ie instance methods) only. A classcast like that is dangerous; if you get it wrong you can have your application stop with Exceptions.
You are trying to access member z of type a and then cast that to type Bc with this piece of code.. (if this worked you would have cast an int to a class ) System.out.println((Bc) a.z);
With this piece of code you are casting an object type a to type Bc and then accessing the member z of type Bc System.out.println(((Bc) a).z); [ September 12, 2008: Message edited by: Gamini Sirisena ]