Explain Encapsulation through an Object Invariant

Read the full interview experience this question came from →

Quick Overview

Explain encapsulation using a reservation invariant and controlled operations. Show why private fields alone are insufficient when setters, mutable references, or concurrent updates can bypass validation.

Explain Encapsulation through an Object Invariant

Company: Apple

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

What is data encapsulation in Java, and how does it help keep an object valid? Give a concrete example rather than only listing private fields, getters, and setters. ### What a Strong Answer Covers Identify an invariant, keep representation private, and expose operations that preserve it. Explain why unrestricted setters or returning mutable internal collections can defeat encapsulation. ### Follow-up Questions - Does a private collection remain protected if a getter returns the same mutable instance? - Why might an operation such as reserve be better than exposing a setAvailable method?

Overview: Explain encapsulation using a reservation invariant and controlled operations. Show why private fields alone are insufficient when setters, mutable references, or concurrent updates can bypass validation.

Read the full Apple Software Engineer interview experience this question came from

Apple logo
Apple
Mar 14, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

What is data encapsulation in Java, and how does it help keep an object valid? Give a concrete example rather than only listing private fields, getters, and setters.

What a Strong Answer Covers Guidance

Identify an invariant, keep representation private, and expose operations that preserve it. Explain why unrestricted setters or returning mutable internal collections can defeat encapsulation.

Follow-up Questions Guidance

  • Does a private collection remain protected if a getter returns the same mutable instance?
  • Why might an operation such as reserve be better than exposing a setAvailable method?