ClassCastException

后端 未结 4 1871
野趣味
野趣味 2020-12-21 00:12

i have two classes in java as:

class A {

 int a=10;

 public void sayhello() {
 System.out.println(\"class A\");
 }
}

class B extends A {

 int a=20;

 pub         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-21 01:00

    B extends A and therefore B can be cast as A. However the reverse is not true. An instance of A cannot be cast as B.

    If you are coming from the Javascript world you may be expecting this to work, but Java does not have "duck typing".

提交回复
热议问题