A a = new B(); // allowed B b = new A(); // not allowed
The first works because it is only a reference to a B object as a type A. The second doesn't work because A is not a subclass of B, and wouldn't fit the description of a B object.
(edit: Basically, you can't call a fruit an apple unless it is an apple, but you can always call an apple a fruit.)
B b = (B)a;
This works because the object referenced by 'a' is actually an object of type B, but is referenced as type A, hence the cast.
(edit: I'm assuming this is consistent with your example code. If you meant that 'a' was actually pointing to an A object, then no, that wouldn't work for the same reason your second line didn't. The only difference is, you would get a ClassCastException at runtime, instead of a compiler error.) [ July 22, 2004: Message edited by: Darin Niard ]
Joined: Jul 22, 2004
Does that means Downcasting is not allowed in Java? As my original question was !!!
Joined: Jun 08, 2004
Why, is this a homework question? I told you how it works. Whether or not that fits your definition of 'downcasting' is for you to decide.