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

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

    when you say B extends A, A becomes father of B now technically B has all charecteristics of A plus its own while A has charecteristics of itself only

    if you say convert A into B and assign to B, that is ok but if you say cast A into B and assign to A, thats not possible as A here does not know extra charecteristics present in B.

    and these things happens at runtime so it will give you a runtime error.

提交回复
热议问题