This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation

 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its recommended to have private instance variable and public methods to change the private instance variable.
1. Whats the idea behind this recommendation rather than having public instance variable and directly accessing it(and change it if need be).
2. Is there any other advantages in terms of security of the code?

The question may look silly; but I just want to get the idea behind it.

Thanks
 
Pavan Sambare
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code, if the variable is public then any external code can modify it directly to any value. You would have no control over it. Whereas if it is modified through a method then you could have some control to validate the new value being set.
For example:
class Person{

private int age;

public void setAge(int age){
if(age > 0){
this.age = age;
}
}
}

Now if age was public, it could have been set to any value. Maybe negative
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an instance variable isn't private, you have no idea who might be accessing your data. Data is king, and everyone likes to maintain control over it.

Once a variable is private, you can make it read only by supplying only a getter. Or write-only by only providing a setter. This is kewl stuff.

Furthermore, you have controll over how a variable is modified or accessed. In the future, you can add to a getter, and perhaps count how many times it is invoked. Or, you can place constraints on the various values your data can take by editing a setter. Using setters and getters provides this flexibility.

And of course, there's just the whole idea that it's not polite to expose your privates. I think that's just a good, all around rule that we all should follow.

Cheers!

-Cameron
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there jib -

I wonder if you could try a little harder to get that last name to match ournaming policy?

Thanks,

Bert
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea behind encapsulation is to support data hiding or protecting data from malicious access.

At the first stage of your program you think that your instance variable can be accessed by anyother class's objects directly. You know that who are going to use your code and how will they use.

Suppose in future you may change your mind that your users must be given some way to access your instance variables. That is, you want to have the control of how to access the variable. You want others to access the variable by providing them a way to access it. No other way other than this. This is an idea about encapsulation.

For better understanding study any Object Oriented Concepts Book.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic