java.lang.ClassException: A cannot be cast into B

后端 未结 13 1257
南笙
南笙 2020-12-31 09:05

I implemented this code:

class A {
    //some code
}
class B extends A {
    // some code
}

class C {
    public static void main(String []args)
    {
              


        
13条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 09:12

    A a1 = (B) new A();
    

    Because A is NOT B.

    Compile time works because you are casting and explicitly guaranteeing compiler that you are sure at runtime A will be B .

提交回复
热议问题