I was wondering what it means to say a field is hidden between 2 java classes and
what it means when running code in terms of resulting output?
I have an abstract class with a protected static boolean field = false and a sub class
which has a boolean field with the same name but is not static and set to true.
If I had this code:
Superclass d = new subclass();
what would be the value of the boolean field in the superclass and the boolean field
in the subclass? Does subclass field stay as false after the assignment above?
staticmembers are never overridden (and certainly not by non-static members). And since you should access them like this:ClassName.memberthere is also no need to worry about hiding them.In your case, you would access the
Superclassfield like this:Superclass.field. And the field of aSubclassinstance like this:subclass.field. If you have, however aSubclassinstance in aSuperclassvariable like above, this code:d.fieldwill access the static field defined inSuperclass, which will befalsein your case.But this does not change the value of the
Subclassinstance, it just accesses the “wrong” member! You can verify this by putting the instance indback into aSubclassvariable and readingfieldagain.