Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

encapsulation in details

 
Geeta Puttappanavar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I’ve a doubt regarding encapsulation. This is the interview question I faced many times but not able to answer properly.
What is encapsulation? I do not need the definition. I need explanation in detail with examples.
Why to use private properties, public setters & getters in encapsulation?

Ex:
1.

--------------------------------------------

=======================================================
2.

-----------------------------------------------------------------------------------


In both cases I can access the properties then why to go for private access modifier???
Please any you clear my doubt.

Waiting for the reply
Thanks,
Geeta
 
Pramod P Deore
Ranch Hand
Posts: 632
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Geeta Puttappanavar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Thanks so much Promod,

"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"

I dint get validation of setter method? can you please explain in details??

Thanks,
Geeta
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Geeta Vp wrote:Why to use private properties, public setters & getters in encapsulation?
Consider a class say User which has a variable long dateOfBirth;
Does it make sense to make it public or even provide a setter method? Can anyone's date of birth change?
 
Geeta Puttappanavar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Geeta Vp wrote:Why to use private properties, public setters & getters in encapsulation?
Consider a class say User which has a variable long dateOfBirth;
Does it make sense to make it public or even provide a setter method? Can anyone's date of birth change?


Maneesh,

For the first time I'm sending message to this forum. I dint know this. Any way thank you so much.

I follow it.

Thanks,
Geeta

 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Geeta Puttappanavar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Hi Manoj,
Thank you so much for your time.
But still my doubt has not been cleared.
So you are telling that 2 properties have to be set in single setter method when the requirement changes
Ex:

In this I should write a code in such way that I should be able to calculate age from DOB (date) & should set???

Regards,
Geeta
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Puttappanavar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


Manoj, I know I’m eating your head. But I wanna my doubt to be clear. Can you please explain with a complete example.
Thanks,
Geeta
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Geeta Vp wrote: Manoj, I know I’m eating your head.

No you are not actually..
I am afraid I told you
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.
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)
the below thread might help you to get the detail.

http://www.coderanch.com/t/383749/java/java/difference-between-abstraction-encapsulation
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can search javaranch as this has already been discussed many times.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic