Data Hiding vs Encapsulation

Naveen AnamaneniUpdated · 4 min read

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

Tickets.java▶ Try it
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());
    }
}
Output
Hidden counter sold: -5
Sell 60: true
Sell 50: false
Sell -5: false
Show sold: 60

How it works

  1. HiddenCounter hides its field (private), but the setter accepts anything. "-5 tickets sold" is nonsense, yet nothing stops it. That's data hiding without encapsulation.
  2. EncapsulatedCounter hides the field and owns the rules: the only way to change sold is sell, which refuses overselling and negative counts.
  3. The show can never be overbooked, however the class is used.

Side by Side

Data hidingEncapsulation
What it isRestricting access to dataBundling data with the behavior that uses and protects it
Main toolprivate (and other access modifiers)Classes with private fields plus meaningful methods
FocusSecurity of the data: who can see itIntegrity of the object: it's always valid
Can exist alone?Yes: private fields with open settersIt includes data hiding
Exampleprivate 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

  1. Easy: Turn HiddenCounter into an encapsulated class by replacing setSold with methods that make sense for a ticket counter.
  2. Harder: Add a cancel(int count) method to EncapsulatedCounter. Which rules does it need?

Quick check: Data hiding vs encapsulation

4 questions
1Data hiding is mainly achieved with:
2A class has private fields and a setter for each with no checks. It has:
3Encapsulation focuses on:
4Which is true?

Key Takeaways

Was this tutorial helpful?

Lesson 143 of 482