In the code given below line no. 7 compiles fine however throws ClassCastException at runtime but line number 9 compiles and runs fine. Is line number 9 stating downcasting( I guess no). I think downcasting is related to objects and not references. Please tell me whether I am correct or not.
Are there some special points to remember while downcasting?
You are trying to get a derived class to point to a base class. This is not possible since the derived class may contain operations that the base class does not contain, which would break inheritance among other things.
What you are telling the compiler when you cast is 'Hey compiler I know that the cast is not implicit, but trust me, everything is going to be fine at runtime'. The compiler trusts you and compiles the application. At runtime the application went kaboom because the casting assumption was incorrect.
The compiler will not always trust you though. Try casting a dog to a String.