Bird
Raised Fist0

In a multi-level inheritance hierarchy, which access modifier allows a subclass in a different package to access a superclass field while still preserving encapsulation?

hard🚀 Application Q8 of Q15
OOP & Design Patterns - Encapsulation - Data Hiding, Getters/Setters & Access Modifiers
In a multi-level inheritance hierarchy, which access modifier allows a subclass in a different package to access a superclass field while still preserving encapsulation?
Aprivate
Bprotected
Cpublic
Ddefault (package-private)
Step-by-Step Solution
Solution:
  1. Step 1: Recall access modifier rules

    Protected allows subclass access even across packages, preserving encapsulation better than public.
  2. Final Answer:

    Option B -> Option B
  3. Quick Check:

    Protected grants subclass access beyond package [OK]
Quick Trick: Protected allows subclass access across packages [OK]
Common Mistakes:
MISTAKES
  • Choosing private which blocks subclass
  • Choosing public which is too open
  • Confusing default with protected
Trap Explanation:
PITFALL
  • Candidates confuse default/package-private with protected or overexpose with public.
Interviewer Note:
CONTEXT
  • Tests nuanced understanding of access modifiers in inheritance.
Master "Encapsulation - Data Hiding, Getters/Setters & Access Modifiers" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes