wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exam Cram Question 34 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam Cram Question 34" Watch "Exam Cram Question 34" New topic
Author

Exam Cram Question 34

Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
The question from Exam cram states
In the following code which defines the class BigWidget as extending Widget, the XXXXXX stands for an access modifier for the method setWidth.

Which of the following modifiers, used in line 4 instead of XXXXXX would allow the BigWidget class to access the setWidth method (as in line 10) but prevent unrelated classes in other packages from calling setWidth?
a. private
b. protected
c. blank - i.e., the method declaration would read
void setWidth(int n)
I chose b. But, the books says the answer is b and c.
I chose b because of the word "unrelated." I assumed that a subclass of Widget would be "related" whether they were in the same package or not and therefore should have access to the setWidth method.
Is this just one of those cases where the question is worded improperly or am I missing something?
Thanks in advance for your help.


Hope This Helps
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
The statement, "unrelated classes in other packages..." means classes that are not subclass of (in this cas class Widget).
Here are the basic rules:
1. All classes in the same package will be able to access a method with a 'default' access modifier (one where you don't declare any).
2. The protected access modifier is less restrictive that the 'default access modifier, since it ALSO allow access to methods declared with the 'protected' access modifier.
[ March 06, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Anil Rudraraju
Greenhorn

Joined: Mar 02, 2002
Posts: 7
hi,
I think the answers r both b and c. The way i understand the question it says
which access specifiers "prevent unrelated classes in other packages from calling setWidth".(the important thing to keep in mind is the other packages part)

I think the confusion is with the way the question is framed, anyway...
Here there r 2 things (related/unrelated) and (same package/other package)
and i'am asssuming related classes to be subclasses of Widget and
same package to be default package(since no package statement is specified)
Now....
if XXXX is declared as protected, "unrelated classes in other packages cannot access setWidth"....... true b is correct.
if XXXX is blank(default access), even here "unrelated classes in other packages cannot access setWidth"....... true c is correct.
hope this helps correct me if i'am wrong
Anil
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
Anil,
Your thought process is excellent. I didn't think about it that way. It helped alot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exam Cram Question 34
 
Similar Threads
Local Inner Class
Difference between protected and blank method
Ques on Access Specifiers
Confusion
About conversions and access modifiers