File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exam Cram Question 34" Watch "Exam Cram Question 34" New topic

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="" 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

Joined: Mar 02, 2002
Posts: 7
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)
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
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
Your thought process is excellent. I didn't think about it that way. It helped alot.
I agree. Here's the link:
subject: Exam Cram Question 34
It's not a secret anymore!