Originally posted by Supriya Nimakuri:
what is the putput and why
class Base {}
class Sub extends Base {}
class Sub2 extends Base {}
public class CEx{
public static void main(String argv[]){
Base b=new Base();
Sub s=(Sub) b;
}
}
1) Compile and run without error
2) Compile time Exception
3) Runtime Exception
Regards
At compile-time, a cast to a subclass is allowed.
However, at runtime, you can't cast a reference to a Base object to a reference to a Sub object.
A Base object is not a Sub object.