Private members are always restricted to the class in which they were declared. Even when there is inheritance, they are not accessible to the child class. This class does not receive the private members of its mother, nor is it able to access the members of the mother through super
. When you opt for a private member, you are saying that it can not leak "under any circumstances" from your class, not even derivatives.
It is not so in any way so, it is possible to do via reflection, but it is a trick to go over the language. This can be abused. So it is not easy to do. It is necessary to understand that the protection that the language gives is only to cover the well-intentioned misuse. It is not a guarantee that there will never be access to members.
Members that are to be accessed by child classes should be declared as protected
. There is inheritance from these members, as does public
, obviously. I think it's obvious also that the protected ones can not be accessed outside the child class.
Another common mistake is to think that the child class accesses the members of another class when there is inheritance. This is not the case. Members of the parent class become part of the child class. You are not accessing elsewhere. There are no two distinct objects, one for the parent instance and one for the daughter instance. There will only be the daughter instance that has all the members, her own and the mother.
It's pretty much as if the compiler copied everything from the parent class and pasted into the child class. You are not seeing the members there in your code, but they are there when you put that is to inherit from a class. Private members are copied, but are not visible to the child class code.
accessor methods are generally public and are inherited. If you do not want them to be public but want the child class to have access to them then they should be protected
. There is no other way. Of course the variable that will support these methods should have at least the same visibility - I would say at the very least the same too, it may not be so, but it makes no sense at all.