Encapsulation and abstraction are often confused. How are they different?
Show the explanation
- Abstraction is about design: decide what an object should expose and ignore unnecessary detail. It answers "what does it do?" and is expressed with interfaces and abstract classes.
- Encapsulation is about implementation: bundle data with the methods that use it and hide the data behind access control, so it cannot be put into an invalid state. It answers "how do I protect how it does it?" and is expressed with
privatefields and methods.
public interface Thermostat { void setTarget(double celsius); } // abstraction: the "what"
public class HomeThermostat implements Thermostat {
private double target; // encapsulation: hidden state
public void setTarget(double celsius) {
if (celsius < 5 || celsius > 35) throw new IllegalArgumentException("out of range");
target = celsius; // the class guards its invariant
}
}A good one-liner: abstraction hides complexity; encapsulation hides data (and protects invariants).
Original UniCircle question. Last reviewed 22 Oct 2026.
