satellite03
IS-IT--Management
Code:
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;
}
}
javac CEx.java // compiled ok
java CEx // runtime exception
Exception in thread "main" java.lang.ClassCastException
at CEx.main(CEx.java:8)
why this error ?
The casting Sub s=(Sub) b; , i think it is a perfectly legal thing.
bcoz it has parent child relationship in the inheritance heirarchy. so, this can not be illegal . then why it is giving "ClassCastException" . one more interesting thing is , compilation has given it a cleanchit but probelm is coming at the runtime !!
i want to know why there is error ?