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

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

    The fact that B extends A means that B is A, i.e. B is like A but probably add some other stuff.

    The opposite is wrong. A is not B. Therefore you cannot cast A to B.

    Think this way. A is Animal. B is Bee. Is Bee animal? Yes it is. Is any animal Bee? Not it is not. For example Dog is animal but definitely not a Bee.

提交回复
热议问题