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 Confusion 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 "Confusion" Watch "Confusion" New topic


Ranch Hand

Joined: Aug 03, 2000
Posts: 169
In the following code which defines the class BigWidget as extending Widget,the xxxxx
stands for an access modifier for the method SetWidth.
1)public class Widget extends Object {
2) protected static final int maxWidth = 40;
3) private int myWidth;
4) xxxxxxx void SetWidth(int n) {
5) myWidth = n;
6) }
8)class BigWidget extends Widget {
9)BigWidget() {
10) SetWidth(maxWidth * 4);
11) }
Which of the following modifiers, used in line 4 instead of xxxxxxx would allow the Big
Widget class to access the SetWidth method(as in line 10) but prevent unrelated classes in
other packages from calling SetWidth?
2)blank-i.e the method declaration would read void SetWidth(int n)
I would select 1 and 2 as the question only asks unrelated classes in other packages but not related classes, so if it is protected related(sub) classes can access,if i say blank can not beaccesed by both related and unrelated classes. What do u guys think? What should we do in the exam if we get such questions?
- Thanks
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think it will be protected only. If it's blank then related classes(sub classes) in other packages cannot access the method.
Praveen Kesani

Joined: Jul 05, 2000
Posts: 24
Default modifier is friendly and it allows access within the package. I was confused with this one too!
I agree. Here's the link:
subject: Confusion
It's not a secret anymore!