03 / 07

What is protected? How is it different from private and public?

Difficulty: 5/10
access modifiers, inheritance, encapsulation

protected vs private vs public

Protected provides controlled visibility between private implementation details and a completely public API. It generally allows access from the declaring class and derived classes, but the exact rules differ between Java and C#. Private is more restrictive and is intended for implementation details of the declaring type. Public exposes the member as part of the externally accessible API. In Java, protected also has package-level access semantics, which makes it broader than simply 'subclasses only'.

javascript
  1. 1

    private: strongest normal member-level encapsulation.

  2. 2

    protected: exposes a member to the declaring type and appropriate subclasses.

  3. 3

    public: exposes the member broadly according to the language's accessibility model.

  4. 4

    Java protected additionally provides access within the same package.

  5. 5

    Protected members can increase coupling between a base class and subclasses.

Scenario Questions

0-2 years experience

  1. 1You have a base class Animal with a protected field age. If you create a subclass Dog in a different package, can it access age? What would change if age were private?
  2. 2In a small utility class you need to expose a method only to classes within the same module but not to external callers. Which access modifier would you pick and why?
  3. 3Suppose you accidentally declared a member as public that should have been protected. What issues could arise in a codebase of a few classes?

2-5 years experience

  1. 1We refactored a parent class and changed a protected method to private, causing a subclass to fail at runtime. Walk me through why the failure happened and how you would fix it.
  2. 2Your team is adding a feature that requires exposing internal state to a set of related classes but not to the rest of the application. Discuss the trade‑offs between using protected versus package‑private visibility.
  3. 3During debugging you notice a subclass cannot see a field defined in its superclass even though you expected it to. What could cause this and how would you verify the visibility rules?

5-8 years experience

  1. 1Design a plugin architecture where plugins extend a core framework class. How would you use protected members to let plugins customize behavior while preventing external code from misusing internal APIs? Discuss potential pitfalls at scale.
  2. 2In a large codebase many classes expose protected fields that are accessed directly, leading to fragile coupling. Propose a refactoring strategy that balances encapsulation with extensibility, and explain the impact on compile‑time dependencies.
  3. 3A protected method is called frequently from many subclasses in a performance‑critical component. What are the implications for JIT inlining compared to a private method, and how would you decide which visibility to use?

8+ years experience

  1. 1Your organization is migrating a monolithic Java application to a modular microservice architecture. How would you redefine access modifiers across module boundaries to keep encapsulation while allowing necessary extension points?
  2. 2When publishing a public API library, you need to expose certain extension points for third‑party developers but hide internal implementation details. Explain how you would use protected, package‑private, and public, and what build‑time checks you’d put in place to enforce the intended usage.
  3. 3A cross‑team initiative wants to standardize a base class hierarchy for data models. What guidelines would you set for when to use protected members versus private with getters/setters, considering future maintainability and binary compatibility?

Follow-up Questions

  • Can you show a quick code snippet that illustrates the difference?
  • What problems have you seen when protected members are over‑used?
  • How would you test that the visibility behaves as expected?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.