File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Encapsulation" Watch "Encapsulation" New topic
Author

Encapsulation

Albert Gray
Ranch Hand

Joined: Mar 06, 2001
Posts: 43
What are best access modifiers (public, private, default, protected.....) to express Encapsualtion in a program?
Albert
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
My guess would be it depends on what you will be creating in the future.
Private is good if you are going to have full control of the member. You can change it later and not worry about who is using it.
Protected would be if you were to allow the subclassed classes access to the member, and members in the package.
public well, that gives rights to everyone.
Anyways, I hope this helped.
Trevor Green
Ranch Hand

Joined: May 30, 2001
Posts: 44
Ecapsulation basically means you have all your member variables private, so no-one else can see them and provide get and set methods to retrieve and set their values:
i) returnType getVariable() {}
and
ii) void setVariable(value){}
I suppose, as mentioned above, the access modifiers for these methods could be public, protected or the default (accessable only to classes within the same package.)
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4744
    
    7

See this article: "Encapsulation is not Information Hiding" in JavaWorld to learn more about encapsulation.
Encapsulation involves a whole lot more than private variables and getters/setters. Look around the OO, Patterns UML, Refactoring forum for some interesting discussion on encapsulation.
Junilu
(added title of JavaWorld article to link text)
[This message has been edited by JUNILU LACAR (edited June 02, 2001).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Prakshi Chopra
Greenhorn

Joined: Mar 05, 2001
Posts: 10
To take use of encapsulation you should use Private access modifier. by giving following two methods.
getxxx()
setxxx(Type a)
now u can use any access modifiers on these methods depending on whom u want to have the access to these variables.
using these methods u have a controlled validation on your variables, so that even accidently no one should assign any wrong value to them.
eg. u want to create a variable studentAge and want to give its access to everyone then defind above methods as public but inside methods u can give code to control the valuse assigned to them like following
private studentAge;
private defaultAge = 19;
setStudentAge(int studentAge){
if( !(studentAge<5) && !(studentAge>80))
this.studentAge=studentAge;
else { this.studentAge = defaultAge; }
}

Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Encapsulation has nothing to do with the access modifiers. This is a common misconception with people learning OOP. Encapsulation means that your data and the methods that operate on that data should be bundled together. The use of getters/setters in a program is information hiding. It is good practice, but doesn't create encapsulation.
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
Originally posted by Angela Ann:
Encapsulation has nothing to do with the access modifiers. This is a common misconception with people learning OOP. Encapsulation means that your data and the methods that operate on that data should be bundled together. The use of getters/setters in a program is information hiding. It is good practice, but doesn't create encapsulation.

I thought that encapsulation meant that data and methods should be bundled AND the implementation of the data should be hidden from the user of the object. If that's so, then encapsulation incorporates information/data hiding--the idea is that one object should not directly access/ manipulate the "internal" data of another. In that case, using getters/setters would be one way to accomplish encapsulation.
Am I incorrect in my belief that the concept of encapsulation as understood in OOP generally covers BOTH functionality as well as data?
Angela Lamb
Ranch Hand

Joined: Feb 22, 2001
Posts: 156
Ranjan, I think you are incorrect. I have a degree in Computer Science and was always taught that encapsulation and information hiding are two SEPARATE concepts. I am including links to articles that help explain this better. Good object-oriented programming should use both together, but they should not be confused as being the same.

http://xarch.tu-graz.ac.at/publ/tutorial/java/tutorials/3797/Java004.htm#encapsulation in general
http://www.elj.com/eiffel/ij/encapsulation/

(Edited for clarity, and to mention that the JavaWorld article posted above by Junilu does an excellent job of showing the differences between these two concepts.)
[This message has been edited by Angela Ann (edited June 01, 2001).]
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
Thanks Angela! I've looked at your links and will read the material carefully.
I also found brief definitions, e.g.:
http://whatis.techtarget.com/definition/0,289893,sid9_gci212060,00.html
In this sense, it suggests that data hiding ENFORCES encapsulation, so that the two are not completely unrelated. So maybe some people have used the term to include data hiding.
Ranjan Chaudhuri
Ranch Hand

Joined: May 22, 2001
Posts: 33
Angela, I also found this essay to be helpful--it explained things to me quite adequately with regard to this question.
http://www.itmweb.com/essay550.htm
Thanks again.
dipak chand
Greenhorn

Joined: May 28, 2001
Posts: 5
As for u r question goes best acess modifier for encapsulation
is protected because allow the subclassed classes access to the member, and members in the package.

deepak
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4744
    
    7

Again, as Angela and I have pointed out: encapsulation has nothing to do with access modifiers. You could make things private and still break the object's encapsulation with getters and setters. It could also be argued that having protected members allows subclasses to break the superclass' encapsulation. You could also have public access to members and yet not break encapsulation. I suggest you go to www.javaworld.com and search for articles on the topic or do a search on Bertrand Meyer and read some of his writings.
As for the SCJP, a question like the one originally asked is not likely to come out in the exam.
Junilu Lacar

Originally posted by dipak chand:
As for u r question goes best acess modifier for encapsulation
is protected because allow the subclassed classes access to the member, and members in the package.
deepak

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