You probably expect the fields to be overridden as a method, with dynamic dispatch based on the type of runtime of the object.
This is not how Java works. Fields are not redefined, they are hidden. This means that an object of class B has two fields with the name "s", but access to which of them depends on the context.
Why is this: in fact, it would be useless to redefine the fields, since there is no useful way to make it work when the types are different, and there is simply no sense when the type is the same (as you can just use the superclass field). Personally, I think this is just a compiler error.
source share