Hi,
I am assigning and array of type Dog to an array of Animals (implicit upcast - Dog extends Animal), and I am getting different results than I expect. Would someone please explain why the array of Animals doesn't receive the Dog values (code below)?
a[x] is defined to be a reference to an Animal. The fact that it happens to point to a Dog object at runtime doesn't change that. Member variables are not runtime polymorphic. Only non-private, non-static, non-final methods are. So, for instance, you could replace the name variable with a getName() method, and have Animal return "Animal" and Dog override it to return "Dog".
In general, non-private member variables are a bad idea, as is naming a member variable in a subclass the same as a variable that's accessible in an ancestor class.
Tomorrow is the first day of the new metric calendar. Comfort me tiny ad:
a bit of art, as a gift, that will fit in a stocking