Data Hiding vs Encapsulation
The two terms are often used as if they meant the same thing, and interviewers like to ask for the difference. Data hiding is one technique: making fields inaccessible from outside, usually with private. Encapsulation is the bigger idea: bundling the data with the behavior and rules that keep it valid. You can hide data and still have no real protection, as the example below shows.
Hidden but Not Protected
public class Tickets {
static class HiddenCounter {
private int sold;
public int getSold() { return sold; }
public void setSold(int sold) { this.sold = sold; }
}
static class EncapsulatedCounter {
private final int capacity;
private int sold;
EncapsulatedCounter(int capacity) { this.capacity = capacity; }
public int getSold() { return sold; }
public boolean sell(int count) {
if (count <= 0 || sold + count > capacity) {
return false;
}
sold += count;
return true;
}
}
public static void main(String[] args) {
HiddenCounter hidden = new HiddenCounter();
hidden.setSold(-5);
System.out.println("Hidden counter sold: " + hidden.getSold());
EncapsulatedCounter show = new EncapsulatedCounter(100);
System.out.println("Sell 60: " + show.sell(60));
System.out.println("Sell 50: " + show.sell(50));
System.out.println("Sell -5: " + show.sell(-5));
System.out.println("Show sold: " + show.getSold());
}
}Hidden counter sold: -5
Sell 60: true
Sell 50: false
Sell -5: false
Show sold: 60How it works
HiddenCounterhides its field (private), but the setter accepts anything. "-5 tickets sold" is nonsense, yet nothing stops it. That's data hiding without encapsulation.EncapsulatedCounterhides the field and owns the rules: the only way to changesoldissell, which refuses overselling and negative counts.- The show can never be overbooked, however the class is used.
Side by Side
| Data hiding | Encapsulation | |
|---|---|---|
| What it is | Restricting access to data | Bundling data with the behavior that uses and protects it |
| Main tool | private (and other access modifiers) | Classes with private fields plus meaningful methods |
| Focus | Security of the data: who can see it | Integrity of the object: it's always valid |
| Can exist alone? | Yes: private fields with open setters | It includes data hiding |
| Example | private int sold; | private int sold; changed only by sell(count) with checks |
Common Mistakes
Calling any class with private fields "encapsulated". If every field has an unchecked setter, the rules live outside the class.
Hiding data that callers really need. Encapsulation doesn't mean "no getters"; it means controlled access.
Interview Corner
What is the difference between data hiding and encapsulation?
Data hiding restricts direct access to an object's data, typically by making fields private. Encapsulation is broader: it bundles the data together with the methods that operate on it, and those methods enforce the rules that keep the data valid. Data hiding is one technique used to achieve encapsulation; a class with private fields but unchecked setters hides its data without really being encapsulated.
Practice
- Easy: Turn
HiddenCounterinto an encapsulated class by replacingsetSoldwith methods that make sense for a ticket counter. - Harder: Add a
cancel(int count)method toEncapsulatedCounter. Which rules does it need?
Key Takeaways
Was this tutorial helpful?