aspose file tools*
The moose likes Java in General and the fly likes Encapsulation in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Encapsulation in Java" Watch "Encapsulation in Java" New topic
Author

Encapsulation in Java

Senthil Kumar Sekar
Ranch Hand

Joined: Aug 08, 2010
Posts: 45

Hi All,
I have one basic question in encapsulation.

Encapsulation is achieved by marking the instance variables to private and providing access thru public accessor and mutator methods.

What is the difference of having the instance variable as public and allowing to be accessed and above encapsulation.

How data is secured with encapsulation?

Thanks.

Regards
Senthil Kumar Sekar
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
What is the difference of having the instance variable as public and allowing to be accessed and above encapsulation.



This difference is that client objects do not directly access the variable. Instead client objects "ask" the object for it by calling a method and the object gives it the value. This is beneficial in many ways, especially in cases where variables types may change or if additional logic later needs to be added to the action of providing the value to client objects. This form of encapsulation fosters flexibility and makes code more agile.

Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
How data is secured with encapsulation?


Data is more secure in the sense that data can be accessed in a safe way - the same way your bank account is secure because it can be accessed in a safe way. In some classes, there are rules for accessing data so that the class is always in a consistent state. These rules can be defined in a method and everyone is forced to comply with these rules by making the data private and the method public. It's like saying if you want to access this data, you must first comply with these rules. If everyone was allowed to directly access the data without complying with the rules, there is a good chance that the class will be left in an inconsistent state.

Encapsulation also has the advantage that the rules can be modified without having to modify every client.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Senthil Kumar Sekar wrote:Encapsulation is achieved by marking the instance variables to private and providing access thru public accessor and mutator methods.


That's true, but it is far from being a description of encapsulation. It's simply a rather trivial example of encapsulation in action.

So asking questions about that example and drawing conclusions about encapsulation doesn't make sense. It's like saying "Nepal is a country" and then drawing conclusions about all countries based on what you know about Nepal.
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Good point Paul. The sentence is poorly written. To be more concise, it could have been written as:

One common method of implementing "encapsulation" is to declare instance variables as private and provide access through public accessor and mutator methods.
Megha kharde
Greenhorn

Joined: Nov 24, 2010
Posts: 19
i would like to add one point
by making instance variables private we can protect them from assigning them to unwanted values . In public accessor method (in setters) we can
add the code to check for allowed values.
For example, for a student we do not want to set its roll no to a negative value

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulation in Java