We always say that data will be encapsulated if we simply define variables private and define getters setters to access those variables. My question is if we can access the variables (data) though via getters and setters, how come data is hidden or safe?
I googled a lot for an explanation, but I found nothing. Every one just said in their blogs and posts that it is a data hiding technique, but it has not been explained/elaborated.
The way I understand your question is, although we declare variables as
private, as those variables can be accessed using getters and setters, they are not private. Therefore, what is the meaning of doing that?Well, when using getters and setters, you can restrict access to the
privatevariables.I.e.,
And the same for the setters.