Java terminology for differentiating runtime type from compile-time type

前端 未结 8 1695
孤街浪徒
孤街浪徒 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:34

    The terminology you are looking for is the Apparent Type and the Actual Type.

    A a = new B();
    

    The Apparent Type is A because the compiler only knows that the object is of type A. As such at this time you cannot reference any of the B specific methods.

    The Actual Type is B. You are allowed to cast the object (that is change its apparent type) in order to access the B specific methods.

提交回复
热议问题