Bird
0
0

You want to allow any class in any package to access a method named calculate inside your class. Which access modifier should you use and why?

hard📝 Application Q8 of 15
Java - Packages and Access Control
You want to allow any class in any package to access a method named calculate inside your class. Which access modifier should you use and why?
AUse default (no modifier) to allow package-only access.
BUse <code>private</code> to restrict access to the class only.
CUse <code>protected</code> to allow access only to subclasses.
DUse <code>public</code> so all classes can access it.
Step-by-Step Solution
Solution:
  1. Step 1: Understand access needs

    The method must be accessible from any class in any package.
  2. Step 2: Match access modifier to need

    Only public allows access from all classes everywhere.
  3. Final Answer:

    Use public so all classes can access it. -> Option D
  4. Quick Check:

    Public = global access [OK]
Quick Trick: Public modifier grants universal access [OK]
Common Mistakes:
  • Choosing protected or default for global access
  • Confusing private with public

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes