Bird
0
0

In a package, you have a class with a method declared with default access. You want to allow only subclasses in other packages to access this method. What should you do?

hard📝 Application Q9 of 15
Java - Packages and Access Control
In a package, you have a class with a method declared with default access. You want to allow only subclasses in other packages to access this method. What should you do?
AChange method access modifier to protected
BKeep default access modifier
CChange method access modifier to private
DChange method access modifier to public
Step-by-Step Solution
Solution:
  1. Step 1: Understand default access limitation

    Default access restricts method visibility to the same package only.
  2. Step 2: Allow subclass access outside package

    Protected access modifier allows subclasses in other packages to access the method.
  3. Final Answer:

    Change method access modifier to protected -> Option A
  4. Quick Check:

    Protected allows subclass access across packages [OK]
Quick Trick: Use protected for subclass access outside package [OK]
Common Mistakes:
  • Assuming default allows subclass access outside package
  • Choosing private which restricts access to class only
  • Choosing public which is too open

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes