posted 15 years ago
Duran,
(B)(A) b;
First casts a B type reference to the A reference type, obtaining an A type reference. This is guaranteed to both compile and run without error (upcasting.) Second, cast the A type reference to the B reference type. This is guaranteed to compile, but may or may not throw an exception based on the actual type of the object (downcasting.) Since the type of the object referred to by b is B, in this case the casting works, and you end up with the same reference you started with (b, a reference of type B pointing to an object of type B.)
All code in my posts, unless a source is explicitly mentioned, is my own.