Interview Q&A · Java · 5 questions in this topic

Encapsulation in Practice and Reference Variables — Java Interview Q&A

All Java topics

How Java enforces encapsulation with access control, and what reference variables actually hold. Part of Objects, References & Memory · 294 questions in the Java bank.

How is encapsulation done in Java?

Answer

Encapsulation is done by declaring fields private and exposing them only through public methods such as getters and setters. The setter is the natural place to validate input so the object can never enter an invalid state. Immutable types and records are an even stronger form because there is no setter at all after construction.

Example

Example.javajava
public class User {
    private int age;
    public int getAge() { return age; }
    public void setAge(int age) {
        if (age < 0) { throw new IllegalArgumentException("age must be non-negative"); }
        this.age = age;
    }
}

💡 Mention records as the modern shortcut for immutable data carriers.

What are reference variables?

Answer

A reference variable holds a reference to an object, or null when it points to nothing. One variable can point to many different objects over time as it is reassigned, while a final reference is locked to the first object it receives. A reference can be typed as a class or as an interface, in which case it can hold any object whose class implements that interface.

Key points

  1. 1A reference has exactly one declared type but can be repointed to other objects.
  2. 2A final reference cannot be repointed, though the object itself may still be mutable.
  3. 3An interface-typed reference can hold any implementing class instance.

Example

Example.javajava
Employee emp = new Employee(); // emp is the reference, new Employee() is the object
Runnable task = new Thread(); // interface-typed reference to an implementing object

Is encapsulation just writing getters and setters?

Answer

No. Getters and setters are a mechanism; encapsulation is the guarantee that an object’s state cannot reach an invalid value. A class with a public setter for every field, each assigning blindly, exposes exactly the state a public field would. Real encapsulation keeps fields private, exposes only the operations that make sense, validates in one place, and returns copies rather than internal mutable objects so callers cannot bypass the rules.

Key points

  1. 1The goal is invariant protection, not accessor generation.
  2. 2A blind setter is as exposed as a public field.
  3. 3Expose behaviour such as deposit(amount), not storage.
  4. 4Return defensively copied collections and arrays.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits