where does downcasted object point to?
public class Animal{
int n = 5;
public static void main(String[] args) {
Animal a = new Animal();
Animal ah = new Horse();
Horse h = new Horse();
System.out.println(h.n); // prints 7
System.out.println(ah.n); // prints 5
h = (Horse) ah;
System.out.println(h.n); // prints 7
}
}
class Horse exten开发者_高级运维ds Animal{
int n = 7;
}
My question:
Why does h.n
still print 7 after h = (Horse) ah
? After the assignment it should point to the same object that ah
points and the n field points to 5?
First, let's call the field n
of the class Animal
"Animal.n
" to avoid confusion.
Fields, unlike methods, are not subject to overriding. So in your Horse class, where you may think you are overriding the value of Animal.n
with 7, you are actually declaring a new variable called n
(let's call it Horse.n
to avoid confusion).
So really, you have a class called Horse
with two fields: Animal.n
and Horse.n
. Which field you get when you say "n
" depends upon the static type of the variable at the time.
When you have an object whose type is Horse
, but upcast to an Animal
, the n
field refers to Animal.n
, and has a value of "5". Hence ah.n
is "5".
When you have the same object, downcast again to a Horse
, the n
field refers to Horse.n
, and has a value of "7". Hence h.n
is "7".
To clarify: Indeed, h
does point to the same object that ah
points to -- downcasting does not change what object is being pointed at. However, the static type does affect which field of the object is being requested.
Every object you create (called an instance) in Java points to the class from which it was created. This is the true type of the object and doesn't change when you cast a reference to it. Similarly, every variable that references an object has a declared type. This is how the compiler will treat the object and determines what operations are allowed.
However, these two types are only loosely coupled. Casting object references changes only the declared type. It doesn't affect how the object behaves--only how the compiler treats it.
精彩评论