I am a bit confused about exactly what it means for a member variable to be inherited. If you have a class with an encapsulated private member variable with a public getter/setter, a subclass can still call the public getter/setter. However, the public getter/setter accesses the private variable of the superclass - so the subclass does have some sort of access to the private variable (and it does I have tested it).
However in my book (Sierra/Bates) and various threads, it says private members are never inherited.
Can someone explain (or point me to a good thread/website that explains) exactly what is happening when members are/are not inherited. I thought I understood it before I thought of the above senario.
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.