Private members are always restricted to the class in which they were declared. Even when there is inheritance, they are not accessible to the daughter class. This class even welcomes the private members of its mother, but cannot access them in the code of the child class, nor can it access the members of the mother through super
. When opting for a private member is saying that he can not leak "under any circumstances" from his class, not even the derivatives.
It is not so at all possible to do it via reflection, but it is a trick to go over the language. This can be abused, so it is purposely not easy to do. It is necessary to understand that the protection that language gives is only to cover the well-meaning misuse. It is not a guarantee that there will never be access to members.
Members that must be accessed by child classes must be declared as protected
. There is an inheritance of these members with visibility in it, as well as the public
, obviously. I think it is also obvious that the protected cannot be accessed outside the daughter class.
Another common mistake is to think that the child class accesses members of another class when there is inheritance, this is not the case. The mother class members become part of the daughter class, not accessing elsewhere. There are no two distinct objects, one for the mother instance and the other for the daughter instance, there will only be the daughter instance that holds all the members, their own and the mother’s.
Well, roughly speaking, it’s like the compiler copied everything from the mother class and glued it to the daughter class. You’re not seeing the members there in your code, but they’re there when you put that down to inherit from a class. Private members are copied, but not visible to the child class code.
The methods accessors are generally public and are inherited. If you do not want them to be public but want the daughter class to have access to them, then they should be protected
, there’s no other way. And of course the variable that will support these methods must have at least the same visibility - I would say at most the same too, it may not be so, but it doesn’t make any sense.
I had this doubt because it is often put as default values for attributes with initialization in constructors. I thought that if I inherit a class that has these initializations in the constructor and I give super in the daughter class I could not get those initialized values from the mother class?
– Aline
So if they go
public
orprotected
, they will be in the daughter class and can access them like this. If they areprivate
, there is no way to access them directly by the daughter class, can through a methodsuper
, I’m not sure I understand that question. I’m not sure I have. When so try to put one in the question a concrete example of what you are trying to do, so it is easier to visualize and propose an alternative.– Maniero
OMG! Now I understand the builder is public.
– Aline
In general, yes, it is possible to do it privately, but in a few cases this is useful.
– Maniero