skip to content

How does information hiding relate to encapsulation, and is a class with public fields encapsulated?

level: middleimportance: should knowfreq 55%

answer

  1. encapsulation = bundle data + behavior
  2. information hiding = restrict access (private)
  3. bundling without hiding = public fields
  4. hide behavior, not just expose accessors
  5. records: hiding + immutability for plain data

basics

~20 s

Encapsulation means bundling data with the methods that work on it inside a class. Information hiding is the part where you keep that data private so it's reached only through methods. A class with public fields bundles data and code but doesn't hide anything, so it's only weakly encapsulated.

solid answer

~50 s

Encapsulation and information hiding are related but not identical. Encapsulation is the OOP principle of bundling state and the behavior that operates on it into a single unit (the class) and exposing a coherent interface. Information hiding is the specific discipline of restricting access to internal details — in Java, making fields private and controlling access through methods. You can have the bundling part without the hiding part: a class with public fields is technically encapsulated in the bundling sense but provides no hiding, so callers depend on its representation and can violate invariants. The real value comes when both are present: the class is the single place that owns its data and the rules about it. So 'is a public-field class encapsulated?' is best answered: it bundles but doesn't hide, which defeats most of the benefits — reduced coupling, enforceable invariants, freedom to change representation.

go deeper

for a junior

Can say encapsulation means putting data and methods together and that private fields are part of it; may not separate the two ideas precisely.

for a middle

Distinguishes bundling (encapsulation) from access restriction (information hiding) and explains why a public-field class is weakly encapsulated.

for a senior

Argues for hiding behavior over exposing data, recognizes that leaking implementation types breaks hiding even with private fields, and uses records for invariant-free data.

for a principal

Applies the distinction at module/API scale — exposing interfaces and stable contracts, hiding implementation packages, and reasoning about coupling and evolvability across teams.

## Two ideas often blurred together These terms are used loosely, but pulling them apart clarifies a lot of interview confusion. ### Encapsulation **Encapsulation** is the object-oriented principle of **bundling** an object's **state** (its data) together with the **behavior** (methods) that operates on that state, inside one unit — the class — and presenting it through a coherent interface. The emphasis is on *cohesion*: the data and the operations that belong together live together, and the class is the unit you reason about, pass around, and reuse. ### Information hiding **Information hiding** is the narrower discipline of **restricting access** to an object's internal details so the outside world can't see or depend on them. In Java this is concretely done with **access modifiers** — primarily making fields `private` and exposing only the methods callers genuinely need. The emphasis is on *concealment*: hide the representation and implementation so callers depend only on a stable contract. ### How they fit together Think of it as: encapsulation = "put the data and behavior in one box"; information hiding = "don't let people reach inside the box." The two reinforce each other and in everyday speech "encapsulation" usually implies both. But they're separable, which the next question exposes. ## The litmus test: a public-field class ```java public class Point { public int x; // public field public int y; } ``` - **Is it encapsulated (bundling sense)?** Loosely yes — `x` and `y` are bundled together, and you could add methods that use them. It's a unit. - **Does it hide information?** No. The representation (two `int`s named `x` and `y`) is fully exposed. Any caller can write `p.x = -5` with no validation, and if you ever wanted to store the point in polar coordinates instead, every caller breaks. So a public-field class has the *form* of a class but throws away the main *benefits*: it can't validate, can't enforce invariants, can't change its representation, and tightly couples callers to its internals. That's why "is it encapsulated?" is a slightly trick question — the honest answer is "it bundles but doesn't hide, so it gives up most of what encapsulation is for." ## Why the distinction matters in practice - It explains why "I made the fields private" plus a leaking getter (see the defensive-copy question) is still poor hiding even though it's nominally encapsulated. - It explains why exposing internal types or implementation classes through a public API breaks hiding even when each individual class has private fields. - It guides API design: encapsulate *behavior*, not just data — the strongest hiding exposes operations (`deposit`, `transfer`) rather than raw accessors. ## A note on records and value-like data Sometimes a pure data carrier with no invariants (like a coordinate pair) genuinely has nothing to hide. The modern idiomatic answer there is a **record**: `record Point(int x, int y) {}`. Its components are private final fields with public accessors and it's immutable — so it provides hiding *and* immutability with no boilerplate, which is strictly better than public mutable fields even for 'dumb data.'

  • If a class only has public getters and setters for every private field, is that strong encapsulation?
    It's better than public fields but still weak: a full get/set pair re-exposes the representation through methods, coupling callers to it. Strong encapsulation hides behavior — exposing meaningful operations and only the accessors that are truly needed, often without setters.

saying these in an interview costs you the question

  • Treating encapsulation and information hiding as exact synonyms with no distinction.
  • Claiming public fields give full encapsulation because the data lives in a class.
  • Equating 'has private fields' with 'well encapsulated' while leaking implementation types or mutable references.

context