aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Must all getters and setters be public to be well encapsulated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Must all getters and setters be public to be well encapsulated? " Watch "Must all getters and setters be public to be well encapsulated? " New topic
Author

Must all getters and setters be public to be well encapsulated?

Seb Jones
Greenhorn

Joined: Aug 18, 2009
Posts: 4
Hi,

According to the SCJP Sun Certified Programmer for Java 6 Study Guide it states:

If you want maintainability, flexibility, and extensibility (and of course, you do), your design must include encapsulation. How do you do that?
- Keep instance variables protected (with an access modifier, often private).
- Make public accessor methods, and force calling code to use those methods rather then directly accessing the instance variable.
- For methods, use the JavaBeans naming convention of set<someProperty> and get<someProperty>.


So I'm under the impression that for encapsulation all instance variables must be defined as private and all getters and setters must be public. However, it seems perfectly reasonable to me that one might want to only grant access to the getters and setters to other classes in the same package or through inheritance (i.e. a subclass). So in this instance could the getters and setters not be marked as protected?

Also, is a "well encapsulated" class, a class that has its instance variables marked as private (not any other access level) or does there have to be getters and setters as well?

Someone has asked a similar question wrt an exam question on encapsulation but the answer was not found: http://www.coderanch.com/t/538247/java-programmer-SCJP/certification/Confusing-Encapsulation-validation

I'm currently revising for the OCJP certification and want to make sure that I don't hit a problem like the person in the other post.

Thanks.

BebopSong


There's no such thing as too much coffee.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Seb Carss wrote:So in this instance could the getters and setters not be marked as protected?


All getter and setter methods are meant to public.Is there any mean to make them protected ?

if you want protect some codes then better to write separate methods for that, don't mix it with getter and setter methods.

Also, is a "well encapsulated" class, a class that has its instance variables marked as private (not any other access level) or does there have to be getters and setters as well?


Yes, for well encapsulation you have to provide getter and setter for them.

OCA7
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Hi, Seb, and welcome to Javaranch!

I'm getting the impression that you think a class should always provide accessor methods for any and all instance variables it declares? That is absolutely not the case. If you don't have to conform to the JavaBean spec, you shouldn't just start adding accessors becasue "that's how you're supposed to do it". JavaBean spec aside, if you do want to provide an accessor with a visibility modifier other than public, that's perfectly fine. Like you said, it can be valuable to limit access to other classes in the same package. This is sometimes used to facilitate testing. Using this approach a class can provide protected test-hooks that may be used by a test class located in the same package.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Rahul mir
Ranch Hand

Joined: Oct 31, 2012
Posts: 38

hi ,
as per your topic Must all getters and setters be public to be well encapsulated?

for well encapsulated , it is 100% required to be public.

and your concerns about this topic http://www.coderanch.com/t/538247/java-programmer-SCJP/certification/Confusing-Encapsulation-validation , SCJP/OCP JP doesn't check your ability that how good you are in programming or in OOPs concept, rather it checks that how you can be out from the situation 'where you have to select best possible options'(this is one of the benchmark of the exam to evaluate a candidate). In practical industry projects, sometime or in many-time "not surprise" you will find bad practice of OOPs, but the application works fine and giving good revenue to company.

so, Try to find the best option whatever you have in current situation, because you can't add another option :-) .

Keep all concept in mind, and try to eliminate options those are not matching with concept .
:-)
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Rahul kumar wrote:
as per your topic Must all getters and setters be public to be well encapsulated?

for well encapsulated , it is 100% required to be public.


No, it's not.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Jelle Klap wrote:
Rahul kumar wrote:
as per your topic Must all getters and setters be public to be well encapsulated?

for well encapsulated , it is 100% required to be public.


No, it's not.


I believe for this exam it is.We have to chose optimal option among some doubtful.Any confusion could lead wrong answer.
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

ankita modi. wrote:
Jelle Klap wrote:
Rahul kumar wrote:
as per your topic Must all getters and setters be public to be well encapsulated?

for well encapsulated , it is 100% required to be public.


No, it's not.


I believe for this exam it is.We have to chose optimal option among some doubtful.Any confusion could lead wrong answer.


No, because the statement simply isn't true, not in context of the OP's question nor in context of the question in the topic he linked to.
Using visibility modifiers other than public for accessor methods does not decrease the level of encapsulation.


Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Jelle Klap wrote:
ankita modi. wrote:
Jelle Klap wrote:
Rahul kumar wrote:
as per your topic Must all getters and setters be public to be well encapsulated?

for well encapsulated , it is 100% required to be public.


No, it's not.


I believe for this exam it is.We have to chose optimal option among some doubtful.Any confusion could lead wrong answer.


No, because the statement simply isn't true, not in context of the OP's question nor in context of the question in the topic he linked to.
Using visibility modifiers other than public for accessor methods does not decrease the level of encapsulation.




Yea, data must be private.
Seb Jones
Greenhorn

Joined: Aug 18, 2009
Posts: 4
Thanks for the responses! I have a couple of points to raise.

1.
Jelle Klap wrote:I'm getting the impression that you think a class should always provide accessor methods for any and all instance variables it declares?


No this is not what I meant as I know sometimes instance variables should be completely private to a class and not have getters and setters or maybe be read only by not having setter methods. What I was trying to get at was what the exam would be expecting. I'm trying to understand the following..

Is a well encapsulated property:
a. One with private access level and public accessor methods
b. One with private access level and public/protected/default accessor methods
c. One with private access level and it doesn't matter about accessor methods
d. One with private/protected/default access level and public/protected/default accessor methods

2.
ankita modi. wrote:Yes, for well encapsulation you have to provide getter and setter for them.


So that would mean, in the exam, if there is a question like the one in the link I posted, the answer would be that it is well encapsulated even though the accessor methods are default access level?

3.
Rahul mir wrote:for well encapsulated , it is 100% required to be public.

ankita modi. wrote:I believe for this exam it is.We have to chose optimal option among some doubtful.Any confusion could lead wrong answer.

Jelle Klap wrote:No, because the statement simply isn't true, not in context of the OP's question nor in context of the question in the topic he linked to. Using visibility modifiers other than public for accessor methods does not decrease the level of encapsulation.


This would make sense wrt to exam question in the link. Also, encapsulation is about restricting access to the properties so that "bad" values cannot be added by other classes without going through a method first and that way if, in the future, a decision is made to add validation to the property no one elses code is going to break.

Thanks for the help.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

From my understanding, you do provide getter and setter methods or not, and what is their access specifier, doesn't matter.For well encapsulation your all data must be private.
Seb Jones
Greenhorn

Joined: Aug 18, 2009
Posts: 4
Great. Thanks all for the help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Must all getters and setters be public to be well encapsulated?