Java — Restrict method access to only other members of the same class

Difficulty: Easy

Correct Answer: private

Explanation:


Introduction / Context:
This checks understanding of access modifiers and their scope rules in Java.



Concept / Approach:

  • private → accessible only inside the same class.
  • protected → accessible in the same package and subclasses.
  • default (no modifier) → package-private access.
  • public → accessible everywhere.



Step-by-Step:

We want strict restriction: "only the same class".private is the most restrictive and satisfies this.


Why Other Options Are Wrong:

  • final: restricts inheritance, not access.
  • static: relates to class-level association.
  • protected: allows subclass and package access, which is broader.
  • volatile: applies to variables only, not methods.


Final Answer:
private

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion