• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access modifier question from Khalid's mock exam

 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is the question
"It is desirable that a certain method within a certain class can only be accessed by classes that are defined within the same package as the class of the method. How can such restrictions be enforced?"
The selection controls are radio buttons (ie: select only one)
The answer options are
1. Mark the method with the keyword Public
2. Mark the method with the keyword protected
3. Mark the method with the keyword private
4. Mark the method with the keyword package
5. Do not mark the method with any access modifiers
He probably will accept only one of them as the right answer.
Which one is it ?
 
naveen sahu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sahir Shah:

This is the question
"It is desirable that a certain method within a certain class can only be accessed by classes that are defined within the same package as the class of the method. How can such restrictions be enforced?"
The selection controls are radio buttons (ie: select only one)
The answer options are
1. Mark the method with the keyword Public
2. Mark the method with the keyword protected
3. Mark the method with the keyword private
4. Mark the method with the keyword package
5. Do not mark the method with any access modifiers
He probably will accept only one of them as the right answer.
Which one is it ?

Sahir
let suppose you are walking on a road . and at any moment
you heared a sound like one of these
1) Hey Sahir
2) Hey Shah
3) Hey Sahir Shah
so at which one sound you respond definately, if
you are only allowed to respond only a single voice.
choose the mose appropriate and then you will get
the ans of your question.
(because i think , you are not a person which doesn't know
which modifier is most suitable)
 
Randall Twede
Ranch Hand
Posts: 4481
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in case you still dont know it is answer 5
 
James Inge
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would have thought 2 & 5. Declaring the method as protected gives it package only restriction the same as no modifier.
Am I wrong?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Protected means that in addition to classes in the same package, classes that extend that class would also have access. The correct answer is 5.
[This message has been edited by Thomas Paul (edited December 09, 2000).]
 
S Sivasuthan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected members of the class are accessible to all the classes in the same package like default.
Regards
Siva
Originally posted by Thomas Paul:
No. Protected means only classes that extend that class would have access. The correct answer is 5.

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Sivasuthan,
Your contribution to Javaranch is very important to us.
However we do have a naming policy outlined here.
Please register again with a name which complies to this rule.
Otherwise I'm afraid you may come to the Ranch soon and find that your account has been locked.
Thank You.
[This message has been edited by Angela Poynton (edited December 08, 2000).]
 
Amit Goel
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this question the answer should be 5th choice as a default specifier can do that as protected keyword is given if other classes are subclasses of the class which you define protected.
 
Sahir Shah
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
No. Protected means only classes that extend that class would have access. The correct answer is 5.



If so shoudn't this be illegal?
<pre>
class Bird{
protected void fly()
{
System.out.println("flap, flap, flap");
}
}
public class Test{
public static void main(String[] args)
{
Bird robin = new Bird();
robin.fly();
}
}
</pre>

 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer was misleading. What I meant to say was that protected would also allow classes that extend the class to have access which is not what the original question wanted to happen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic