*
The moose likes Java in General and the fly likes encapsulation in details Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "encapsulation in details" Watch "encapsulation in details" New topic
Author

encapsulation in details

Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
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

Joined: Jul 15, 2008
Posts: 629

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.


Life is easy because we write the source code.....
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
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

Joined: Jul 26, 2007
Posts: 10167
    
    8

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?

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
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

Joined: Aug 22, 2008
Posts: 191

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....
Geeta Puttappanavar
Ranch Hand

Joined: Jan 18, 2012
Posts: 43
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

Joined: Aug 22, 2008
Posts: 191

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

Joined: Jan 18, 2012
Posts: 43
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

Joined: Aug 22, 2008
Posts: 191

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

Joined: Aug 22, 2008
Posts: 191

You can search javaranch as this has already been discussed many times.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: encapsulation in details
 
Similar Threads
Doubt in Object Reference
Inheritance Doubt
exception doubt
Doubt
Problem passing values to a function