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

后端 未结 13 939
南笙
南笙 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

    When B extends A, it means all methods and properties of A are also present in B.

    So you can ever cast B to A,

    but you CANNOT cast A to B.

    You have to be really care about casting in your application.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-31 09:22

    I'm not sure about the compile part, but I can explain the runtime error.

    B extends A, which means that every object of class B, is also an object of type A. The other way around is not true.

    Compare A with 'Mammal', and B with 'Cow'. A Cow is always a Mammal, but not every Mammal is a Cow.

    0 讨论(0)
  • 2020-12-31 09:26

    Variables of type A can store references to objects of type A or its subtypes like in your case class B.

    So it is possible to have code like:

    A a = new B();
    

    Variable a is of type A so it have only access to API of that class, it can't access methods added in class B which object it refers to. But sometimes we want to be able to access those methods so it should be possible to somehow store reference from a in some variable of more accurate type (here B) via which we would be able to access those additional methods from class B.
    BUT HOW CAN WE DO THAT?

    Lets try to achieve it this way:

    B b = a;//WRONG!!! "Type mismatch" error
    

    Such code gives compile time Type mismatch error. It happens to save us from situation like this:

    • class B1 extends A
    • class B2 extends A

      and we have A a = new B1();.

      Now lets try to assign B1 b = a;. Remember that compiler doesn't know what actually is held under variable a so it needs to generate code which will be safe for all possible values. If compiler wouldn't complain about B1 b = a; it should also allow to compile B2 b = a;. So just to be safe it doesn't let us do it.

      So what should we do to assign reference from a to B1? We need to explicitly tell compiler that we are aware of potential type mismatch issue here, but we are sure that reference held in a can be safely assigned in variable of type B. We do so by casting value from a to type B via (B)a.

      B b = (B)a;
      

    But lets go back to example from your question

    B b1 = (B) new A();
    A a1 = (B) new A();
    

    new operator returns reference of the same type as created object, so new A() returns reference of the type A so

    B b1 = (B) new A();
    

    can be seen as

    A tmp = new A();
    B b1 = (B) tmp;
    

    Problem here is that you can't store reference to object of superclass in variable of its derived type.
    Why such limitation exist? Lets say that derived class adds some new methods that supertype doesn't have like

    class A {
        // some code
    }
    
    class B extends A {
        private int i;
        public void setI(int i){
            this.i=i;
        }
    }
    

    If this would be allowed

    B b = (B)new A();
    

    you could later end up with invoking b.setI(42);. But will it be correct? No because instance of class A doesn't have method setI nor field i which that method uses.

    So to prevent such situation (B)new A(); at runtime throws java.lang.ClassCastException.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题