Instance variables cannot be overridden. In this case, the 'arr' variable in 'Sub' class is a new declaration done in Sub, and is NOT an overridden instance variable.
As per rules of method overriding, the overridden method in the subclass should not be more restrictive than the superclass version, right?
But, If you have the subclass code as private int arr = 45; ,it will compile, even though 'arr' has more restrictive privilege in the subclass. This means that 'arr' is not overridden.
On the other hand, if you override public void method() as private void method() in the subclass, you will get a compiler error.
So, this proves that only instance methods can be overridden and not instance variables. But, both instance methods and instance variables are inherited. The property of superclass is the property of subclass also (unless the member is not private)
I think this is referred to as "hiding" or "shadowing".
If you have a superclass and subclass that each contain a variable with the same name, the access is controlled by the type of the reference variable. If the reference variable is super you will access the superclass' variable. If the reference variable is subclass you will access the subclass' variable. Now BOTH of these cases are talking about direct access (refVar.varName style). If a superclass variable is used to reference a method that is overridden in the subclass and it uses the same variable name.... IT will use the subclass variable.
SCJP - 86% - June 11, 2009