What is Encapsulation in OOP?
Learn encapsulation in OOP — private fields, getters/setters and information hiding — with a Java BankAccount example and interview questions.
Expected Interview Answer
Encapsulation is the object-oriented principle of bundling an object’s data with the methods that operate on it and restricting direct access to that data from outside the class.
Fields are typically declared private, and controlled access is provided through public getter and setter methods, which can validate input before allowing state changes. This protects an object from being put into an invalid state and hides implementation details so the internal representation can change without breaking external code that depends only on the public interface. Encapsulation is one of the four pillars of OOP alongside abstraction, inheritance, and polymorphism, and it is the mechanism that makes information hiding possible.
- Prevents invalid or inconsistent internal state
- Hides implementation details from consumers
- Internal changes don’t break external callers
- Provides a single controlled point for validation
AI Mentor Explanation
A team’s official scoresheet can only be updated by the appointed scorer, following the proper recording procedure ball by ball — a spectator cannot walk up and change the score directly. This is encapsulation: the score (data) is bundled with the scoring process (methods) that validate every change, and outside access is restricted to the scorer’s controlled interface. The public sees the final numbers, never a way to tamper with them directly.
Step-by-Step Explanation
Step 1
Make fields private
Declare instance variables as private so they cannot be accessed directly from outside the class.
Step 2
Provide public accessors
Add public getter methods to read state where needed.
Step 3
Provide validated mutators
Add public setter methods that validate input before changing state.
Step 4
Expose only the controlled interface
External code interacts solely through these methods, never the raw fields.
What Interviewer Expects
- Correct definition: bundling data with methods, restricting access
- Mention of private fields with public getters/setters
- Understanding that it prevents invalid state
- Distinction from abstraction (encapsulation is about protecting how, not defining what is exposed)
Common Mistakes
- Defining encapsulation as merely "using getters and setters"
- Confusing encapsulation with abstraction
- Forgetting that setters should validate, not just assign
- Making all fields public "for convenience," defeating the purpose
Best Answer (HR Friendly)
“Encapsulation means keeping an object’s internal data private and only allowing it to be changed through well-defined, validated methods. It protects the object from ending up in an invalid state and lets the internal implementation change later without breaking the code that uses it.”
Code Example
public class BankAccount {
private double balance; // private: no direct external access
public BankAccount(double initial) {
if (initial < 0) throw new IllegalArgumentException();
this.balance = initial;
}
public double getBalance() { return balance; }
public void deposit(double amount) {
if (amount <= 0) throw new IllegalArgumentException();
balance += amount; // change allowed only via validated method
}
public void withdraw(double amount) {
if (amount <= 0 || amount > balance) throw new IllegalArgumentException();
balance -= amount;
}
}Follow-up Questions
- What is the difference between encapsulation and abstraction?
- Why should fields generally be private rather than public?
- What is information hiding?
- Can encapsulation be achieved without access modifiers?
MCQ Practice
1. Encapsulation is best described as?
Encapsulation bundles data with its operations and restricts direct external access to that data.
2. Which access modifier is most commonly used for fields under encapsulation?
Fields are typically private, with controlled access through public methods.
3. A key benefit of encapsulation is?
Controlled access through validated methods prevents an object from reaching an invalid state.
Flash Cards
Encapsulation in one line? — Bundle data with methods and restrict direct access to that data.
Typical mechanism? — Private fields with public getter/setter methods.
Main benefit? — Prevents invalid state and hides implementation details from callers.
Not to be confused with? — Abstraction, which is about what is exposed, not how access is controlled.