Overriding member variables in Java ( Variable Hiding)

There is no polymorphism for fields in Java.

Variables decision happens at a compile time so always Base Class variables (not child’s inherited variables) will be accessed.

So whenever upcasting happens always remember

1) Base Class variables will be accessed.

2) Sub Class methods(overridden methods if overriding happened else inherited methods as it is from parent) will be called.


Variables are not polymorphic in Java; they do not override one another.


When you make a variable of the same name in a subclass, that's called hiding. The resulting subclass will now actually have both properties. You can access the one from the superclass with super.var or ((SuperClass)this).var. The variables don't even have to be of the same type; they are just two variables sharing a name, much like two overloaded methods.