Java Programming - Declarations and Access Control - Discussion

17. 

Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?

[A]. final
[B]. static
[C]. private
[D]. protected
[E]. volatile

Answer: Option C

Explanation:

The private access modifier limits access to members of the same class.

Option A, B, D, and E are wrong because protected are the wrong access modifiers, and final, static, and volatile are modifiers but not access modifiers.


Tino said: (Sep 28, 2011)  
Classes cannot be define as protected. Then how it is possible?

Chronoshd said: (Nov 16, 2013)  
Final, static and volatile are out of the question, protected doesnt change anything and private restricts the access.

Bfhj said: (Dec 2, 2013)  
But we can access the private members in the class which they are defined.

Chad said: (Dec 10, 2013)  
Same comment as @Tino, how is this a valid question? Are we supposed to assume we're talking about a nested class here?

Schad said: (Oct 8, 2014)  
Inner classes can be protected and private.

Khaled said: (Jun 1, 2015)  
I think its not a valid question either. In inner class private method is accessible from other method too.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.