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

后端 未结 13 962
南笙
南笙 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:11

    Has to do with when casting is done. You are telling the compiler: "Hey, don't worry about it, this is what I say it is, if you have a problem, take it up with me at runtime."

    Basically, the compiler is letting you do your thing. When you explicitly cast something, the compiler doesn't do checks. When you run, and the program tries to cast but fails, that's when you will see the error.

提交回复
热议问题