Java terminology for differentiating runtime type from compile-time type

前端 未结 8 1652
孤街浪徒
孤街浪徒 2020-12-06 08:56

In Java, an Object can have a runtime type (which is what it was created as) and a casted type (the type you have casted it to be).

I\'m wondering what

8条回答
  •  有刺的猬
    2020-12-06 09:19

    The type of the variable a is A. There's no changing that, since it's a reference. It happens to refer to an object of type B. While you're referring to that B object through an A reference you can only treat it as though it were of type A.

    You can later cast it to its more specific type

    B b = (B)a;
    

    and use the B methods on that object.

提交回复
热议问题