Java Programming - Declarations and Access Control - Discussion

10. 

You want a class to have access to members of another class in the same package. Which is the most restrictive access that accomplishes this objective?

[A]. public
[B]. private
[C]. protected
[D]. default access

Answer: Option D

Explanation:

The only two real contenders are C and D. Protected access Option C makes a member accessible only to classes in the same package or subclass of the class. While default access Option D makes a member accessible only to classes in the same package.


Girish said: (Jan 20, 2011)  
well explained

Sujit said: (Sep 19, 2012)  
When we are talking about same class, most restrictive access should be Private. Default is restrictive only for different package.

Am I right?

Amaziane said: (Jun 28, 2014)  
But the question concern classes to have access to members of another class in the same package.

Private authorize just members of class to access each other but not outside of this class, if you want, in the same package, to access from class A a class B you have to have at least class B with default member access?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.