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

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

    The name it self implies the compiler will just looks at compile-time type of the expression.

    It does not do assumptions on the runtime type of the expression.

    http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.5.1

    Coming to real problem

    You cannot cast A to B.You can cast B to A.When you have a Mango,You have Fruit.But when you have a Fruit,It not mean that You have a Mango.

提交回复
热议问题