What is the difference between abstraction and encapsulation?
Interview preparation resource from Gate Smashers.
Abstraction presents the essential operations of an object or component while hiding unnecessary implementation details. It focuses on what the component does. Encapsulation bundles state with the operations that manage it and controls access to that state. It focuses on protecting and maintaining the object’s internal state. For example, an interface can abstract a capability such as calculating an area without revealing how each shape performs the calculation. A class can encapsulate its fields by making them private and validating changes through methods. The concepts complement each other: abstraction simplifies how clients use a component, while encapsulation protects its implementation and invariants.
// Abstraction: define a contract for calculating an area.
interface Shape {
double area();
}
// Encapsulation: keep state private and validate updates.
class Circle implements Shape {
private double radius;
public Circle(double radius) {
setRadius(radius);
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
if (radius < 0) {
throw new IllegalArgumentException("radius must be non-negative");
}
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle(2.5);
System.out.println(shape.area());
}
}Abstraction
Abstraction provides a simplified view of a component by exposing a useful contract and omitting details that a client does not need to know. It answers the question, “What can this component do?”
In object-oriented programming, interfaces, abstract classes, and public API contracts are common ways to express abstraction. An abstraction can have multiple implementations without changing the code that depends only on its contract.
- Main goal: Reduce complexity by exposing essential behavior.
- Example: A Shape contract declares area() without specifying how a circle, rectangle, or another shape calculates it.
Encapsulation
Encapsulation groups an object’s data and the operations that work on that data. It also limits direct access to internal representation so the object can control how its state is created and changed.
Access modifiers such as private and protected are language-specific mechanisms commonly used to support encapsulation. Encapsulation does not require getters and setters; behavior-oriented methods are often preferable when they better preserve the object’s rules.
- Main goal: Protect internal state and enforce valid object state.
- Example: A Circle keeps its radius private and validates a new radius before storing it.
How they work together
The two concepts address different concerns and are commonly used together. A client can depend on an abstract contract, while each concrete implementation encapsulates its own state and internal logic.
Encapsulation helps implementations change without exposing internal representation. Abstraction helps clients use a component without depending on a particular implementation.
- Abstraction controls: The operations a client can rely on through a contract.
- Encapsulation controls: Direct access to an object’s internal data and implementation details.
