posted 20 years ago
Private members are never inherited. Keep that in mind. That said, you can always do something like this:
Notice that, in that code, I'm able to access "a" from outside the class it is defined even though (1) the variable "a" is private and (2) the class that is performing the access is in no way related to the class where the private variable is defined.
The whole concept here is that, even though a is private, Encapsulation offers access to it via a public getter method. Notice, however, that there is no corresponding setter method. This means that, while another class may access the value of a, that class could never set the value of a. The concept of public getters/setters for private variables is nice because, in the end, you can control what variables are set to what within the class in which they are defined. If the variables are made public, you have no such control.
Now, back to your question. If a variable were to be truly inherited, you'd be able to access it via its simple name (assuming it isn't being hidden and even then you could still access it, just not with its simple name) without having to go through a public getter/setter method. Take a look at this:
In that snippet, you can see that the first println statement causes an error because the variable "a" is not inherited. Meanwhile, the variable "b" is inherited so it can be access via its simple name.
I hope this helps,
Corey