PrepZone Logo
PrepZone

Encapsulation and Invariants

Protect state with contracts so invalid objects never exist.

Why this matters

  • Invalid object states are design bugs, not caller bugs.
  • Value objects and factories enforce rules at construction time.
  • Interviewers watch whether you expose mutable collections directly.
  • CampusOS domain entities should fail fast when rules break.
EncapsulationHide the data, expose behaviour
AbstractionPublish intent, hide mechanics
InheritanceReuse a parent contract
PolymorphismOne call, many outcomes
Encapsulation and abstraction decide what the outside world can see. Inheritance and polymorphism decide how types relate and which code actually runs.

Overview

Protect state with contracts so invalid objects never exist. Study each section below in order — every list is interview-ready reference material.

Private state

Points to cover

  • Definition — what Private state means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Factory validation

Points to cover

  • Definition — what Factory validation means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Value objects

Points to cover

  • Definition — what Value objects means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design

Fail fast

Points to cover

  • Definition — what Fail fast means in one clear sentence
  • When to use — the force or smell that triggers this concept
  • CampusOS example — where this appears in parking, booking, or library modules
  • How to explain — pointwise bullets you can say aloud in an interview
  • Common mistake — what weak candidates do instead
  • Refactor move — one concrete change that improves the design
Java
// CampusOS — apply "Private state" on paper first, then in code
public final class EncapsulationAndInvariantsExample {
    // 1. Name entities from the problem statement
    // 2. Draw relationships before writing methods
    // 3. Introduce patterns only when a force appears
}

Quick recall

Everything you need if you only revisit this box.

  1. Private state — review this slice before mock interviews.
  2. Factory validation — review this slice before mock interviews.
  3. Value objects — review this slice before mock interviews.
  4. Fail fast — review this slice before mock interviews.

Test yourself

Answer these before moving on — recall is what makes it stick.