Life is easy because we write the source code.....
Pramod P Deore wrote:In case 2nd of your example you can add validation of setter method so another programmer can't set any value that can be create problem in future for your code. But suupose you can't use getter and setter method to set variable value as in case 1 of your code then any value directly assign to your variable. So it is better to use private for variables and public for getter and setter methods.
Consider a class say User which has a variable long dateOfBirth;Geeta Vp wrote:Why to use private properties, public setters & getters in encapsulation?
Maneesh Godbole wrote:Welcome to the Ranch.
In future, while posting code, please UseCodeTags. I have added them for you this time. As you can see it makes the code much more easier to read and understand.
Consider a class say User which has a variable long dateOfBirth;Geeta Vp wrote:Why to use private properties, public setters & getters in encapsulation?
Does it make sense to make it public or even provide a setter method? Can anyone's date of birth change?
Do not wait to strike till the iron is hot; but make it hot by striking....
Manoj Kumar Jain wrote:Encapsulation enables to hide original implementation from the user so that later if there is code/structure changes then that doesn't affect the end user.
If someone is using the code and directly accessing the variables then you have no control how user manipulate these.
But if there is a layer (getter/setter) that limit/validate/hide the details then later implementation can be change without letting user know.
e.g., you are already having the variable Date of Birth in your class that is being set by user directly by dot operator.
what if you introduced one more field age in your class and want to initialize it ? user is not aware of it and you told him then there might be lots of changes at lots of places if he needs to also set the value of age.
if you are having setter method you can update the setter like this.
the only change at one place will suffice.
Do not wait to strike till the iron is hot; but make it hot by striking....
Manoj Kumar Jain wrote:You have mistaken, I didn't mean that 2 properties have to be set in single setter method. neither I meant that the example I provided is the reason for the encapsulation or for getter/setter. There may be many situation where you will feel that its better to use getter/setter.
there are many ways to solve a single problem what I want to say that encapsulation hides the unnecessary details from the user and make it a single entity to use and also makes it simple to make changes without/less impact.
Geeta Vp wrote: Manoj, I know I’m eating your head.
which is the definition of the Abstraction. however use of getter/setter is helpful if something happen as I told or for validation and some more reasons (which I can't recall at now)Encapsulation enables to hide original implementation from the user so that later if there is code/structure changes then that doesn't affect the end user.
Do not wait to strike till the iron is hot; but make it hot by striking....
Quick! Before anybody notices! Cover it up with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|