What is Encapsulation in Java?
Learn encapsulation in Java: private fields, getters and setters, data hiding, and validation. With a clear example, benefits, and interview questions.
Expected Interview Answer
Encapsulation in Java is the practice of bundling data (fields) and the methods that operate on it inside a single class while hiding the internal state behind private access, exposing it only through controlled getters and setters.
By marking fields private and providing public accessor methods, a class controls how its data is read and modified, allowing validation and invariants to be enforced in one place. This data hiding decouples the internal representation from external code, so the implementation can change without breaking callers. Encapsulation is the foundation of maintainable object-oriented design and is closely tied to the principle of least privilege.
- Protects internal state from invalid changes
- Allows validation inside setters
- Hides implementation details from callers
- Makes classes easier to change safely
- Improves maintainability and testability
AI Mentor Explanation
A team's dressing room is off-limits to outsiders; you interact with the side only through the captain and the scoreboard. You cannot walk in and reshuffle the batting order yourself. Encapsulation is that closed dressing room: the fields are private internal state and the public methods, like the captain, are the only sanctioned way to read or change what happens inside.
Step-by-Step Explanation
Step 1
Make fields private
Declare the class's instance variables with the private modifier so outside code cannot touch them directly.
Step 2
Add getters
Provide public accessor methods to read the values in a controlled way.
Step 3
Add setters with validation
Provide public mutator methods that validate input before changing the field.
Step 4
Enforce invariants
Keep all rules about valid state inside the class so external code cannot break them.
Step 5
Expose only what is needed
Keep helper methods and internal details private, following the principle of least privilege.
What Interviewer Expects
- Understanding of data hiding via private fields
- Use of getters and setters
- Validation enforced inside setters
- Difference between encapsulation and abstraction
- A clear code example
Common Mistakes
- Making fields public and calling it encapsulation
- Adding getters and setters with no validation or purpose
- Confusing encapsulation with abstraction
- Returning direct references to mutable internal objects
- Exposing implementation details through public fields
Best Answer (HR Friendly)
“Encapsulation means keeping an object's data private and letting other code use it only through approved methods. In Java you hide fields and provide getters and setters, so the class controls its own state and stays safe and easy to change.”
Code Example
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("Amount must be positive");
}
balance += amount;
}
public void withdraw(double amount) {
if (amount > balance) {
throw new IllegalArgumentException("Insufficient funds");
}
balance -= amount;
}
}Follow-up Questions
- What is the difference between encapsulation and abstraction?
- Why should fields be private instead of public?
- How do getters and setters support encapsulation?
- What problems arise from returning a mutable internal object?
- How does encapsulation relate to the principle of least privilege?
MCQ Practice
1. Which access modifier best supports encapsulation for fields?
Making fields private hides internal state and forces access through controlled methods.
2. What is the main purpose of a setter method?
A setter provides a controlled entry point where input can be validated before the field changes.
3. Encapsulation primarily achieves which of the following?
Encapsulation bundles data with methods and hides internal state, which is data hiding.
Flash Cards
What is encapsulation? — Bundling data and methods in a class while hiding internal state behind private access.
Which modifier hides a field? — private — outside code must go through public getters and setters.
Why use a setter? — To validate input and control how a private field is modified.
Encapsulation vs abstraction? — Encapsulation hides state; abstraction hides complexity by exposing only essential behaviour.