Originally posted by Jothi Shankar Kumar Sankararaj:
Hi Above,
My question was if I can cast a superclass reference to a subclass reference? And also if I do this, I just get a run time wrror rather than compile time? Can you elaborate on this?
Regards,
Jothi Shankar Kumar. S
only when two unrelated classes casting will give you compile-time error. say:
class A{}
class B{}
A a = (A)new b();// this line gives you compile-time error.
if any inheritence hiarachy exists, you won't see error until run-time.
however, interface is the exception. Given:
Interface A{}
class B{}
A a = (A)new B();// this line doesn't give you compile-time error even though B is not implementing A