In Java we're always following Encapsulation rules, but there is one paradox in my mind, in all documents the concept of Encapsulation is defined like this:
Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class.
We're defining all the variables inside the classes as private
to not getting accessed by anyone outside the class.
Again we are creating setters and getters for those private variables, and those variables are getting accessible for anyone outside the class by using setters and getters.
I will appreciate if anyone can help to get clear the concept of Encapsulation for me.
Aren't we just increasing code by declaring private
and creating setters and getters?