aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Encapsulation" Watch "Encapsulation" New topic
Author

Encapsulation

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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

Joined: Aug 21, 2006
Posts: 23
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


SCJP 1.4 -- 98%<br />SCWCD 5 -- 97%
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hey there jib -

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

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
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.


if you think you can you r right<br />if you think you can not you r double right
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulation