Let's start from another behavior: even if you declare method/variable as private, another instance of the same class can access it. It's OK I can live with it. I call these class private and not instance private.
Now the question part:
For example, in runtime I want to be able to check that all String variables in this
class are not null, and if they are null they should be changed to "NULL" string.
I run through the variables in reflection and get their values. But if I extend my class and add private or even protected variables my base class can't access them. I have to setAccessible on the variables before I can use it.
So please explain to me why the base-class (super-class) can't access private/protected variables from it's sub-class. It is its sub-class, so I don't get it. What's the idea behind this?
I know that super-class should not know about it's sub-classes, but in my example it makes sense, no?
Is it because I can't or shouldn't restrict my sub-classes in this way?
Update: Based on the answers, I want to know also: Why accessing another instance's private variables from the same class instance isn't considered violation of encapsulation ?