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

 
Albert Gray
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are best access modifiers (public, private, default, protected.....) to express Encapsualtion in a program?
Albert
 
Charlie Swanson
Ranch Hand
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Prakshi Chopra
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6529
21
Java Linux Mac Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic