pradeep rajkumar wrote:Hi,
When i compiled and ran the below code,
I got a Runtime Exception as below,
Its just an ordinary Downcasting right ?
Then why this Exception was thrown ?
You cannot downcast an actual parent object to a child object. The cast will only work if the reference variable is of the parent type but the actual object being referred to is of the child type. So, the acutal object Tree cannot be cast to a child object (Redwood).
When you put in an explicit cast, you're effectively telling the compiler "I know what I'm doing - trust me, the object is going to be of this type". The compiler believes you, unless you're trying a cast that can't possibly work (like trying to cast a Tree to a Dolphin). If it might work, the compiler will let it through.
Joined: Sep 28, 2011
fred rosenberger many thanks for the reply!
Funny, as trying to learn all the difficult stuff you sometimes miss the obvious and easy stuff!!!