• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion

 
Doit
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) }
7)}
8)class BigWidget extends Widget {
9)BigWidget() {
10) SetWidth(maxWidth * 4);
11) }
12)}
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?
1)protected
2)blank-i.e the method declaration would read void SetWidth(int n)
3)private
4)public
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it will be protected only. If it's blank then related classes(sub classes) in other packages cannot access the method.
Thanks,
BS
 
Praveen Kesani
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default modifier is friendly and it allows access within the package. I was confused with this one too!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic