This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "What is Encapsulation" Watch "What is Encapsulation" New topic
Author

What is Encapsulation

jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
Class MyMarks {
private int vmarks = 0;
private String name;
public void setMarks(int mark)
throws MarkException {
if(mark > 0)
this.vmarks = mark;
else {
throw new MarkException("No negative
Values");
}
}
public int getMarks(){
return vmarks;
}
//getters and setters for attribute name goes here.
}

In the code above , instead of private variables can I have access specifier as protected ? Will it still be encapsulation ?
Doug Braidwood
Ranch Hand

Joined: Apr 04, 2010
Posts: 42
If the variables are private, which other classes can access them directly without using the "get" function?


SCJP, SCWCD
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read.

You can edit your post by using the button.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
In general it means hiding implementation details from the client.


JDBCSupport - An easy to use, light-weight JDBC framework -
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

jose chiramal wrote: instead of private variables can I have access specifier as protected ? Will it still be encapsulation ?


As far I know, Encapsulation is Specific to a class , another class(say user) dont have the rights to change/affect the data(encapsulation). if you make the variable as protected,it visible to a user(if he extends that class), which violate encapsulation. when you say protected, it is mostly related to an Abstraction
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

encapsulation will "shield" your instance variables (state)

say you have an Animal object (class), that may have ints for "eyes", "legs" and so on

You dont want to be setting these values directly elsewhere in your application, for example you dont (but can) want to do:

Animal a = new Animal();

a.eyes = 2

Thats not nice, you want to use encapsulation to shield that variable. So you would have accessors (getters and setters) which will set/retrieve the values for you, so you can say:

Animal a = new Animal();
a.setEyes(2);

That means that every time you set the number of eyes, it goes thru that accessor.

Another better example may be if you have a House object, such as :

House h = new House();
h.setPostcode("ABC123");

At least then, on your setPostcode method(accessor) in your House class, you could introduce a regex, or to format to upper or lower case etc. It introduces a single point of failure, so providing everyone uses the acessors, they should be guaranteed to get the behaviour they expected


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is Encapsulation
 
Similar Threads
core java
doubt on uCertify question
Array Understanding
Q on Kathy's Mock Test (Topic:encapsulation)
Encapsulation