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.
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.
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.)
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).]
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; } }
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.
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
posted
0
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.
(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
posted
0
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
posted
0
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.
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.
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