🚗
OOP

Abstraction

Hiding Complexity
💡 While driving, you only use the steering wheel and pedals — you don't need to see what's happening inside the engine. That's Abstraction!

Abstraction hides complex implementation and shows only what's necessary.

An abstract class or interface lets us state "what to do" — the implementing class decides "how to do it".

abstract class Shape {
  abstract double area();
}
class Circle extends Shape {
  double r;
  double area() { return 3.14 * r * r; }
}
🎛️
While driving, you only use the steering wheel and pedals — you don't need to see what's happening inside the engine. That's Abstraction!
1 / 4
⚡ Quick Recap
  • Hide complexity, show essentials
  • Defined via abstract class / interface
  • State "what", let the implementer decide "how"
On this page (3 subtopics)

You can't directly "new" an abstract class (new Shape() will error if Shape is abstract) — you can only instantiate its concrete (non-abstract) subclasses. It can have both abstract methods (no body, just a signature — abstract double area();) and normal methods (with a body).

Any class that extends it must implement all the abstract methods — otherwise that child class must be abstract too (passing the responsibility further down).

abstract class Shape {
  abstract double area();          // no body
  void display() {                  // normal method, body hai
    System.out.println("Area: " + area());
  }
}

When related classes need to share some common code (like all Shapes having the same display() method — written in one place), and some behavior needs to be defined individually by each child (area() has a different formula for each shape — Circle's formula differs from Square's).

Unlike an interface, an abstract class already has "half-done" implementation — so it's the best fit when classes are genuinely related (a strong IS-A relationship), not just sharing one common capability.

These are two different concepts that often get confused, especially in interviews. Encapsulation is about "how" (protecting/bundling data, private+getter/setter) — an implementation-level detail.

Abstraction is about "what to show" (hiding complexity, showing only the essential interface) — a design-level decision. Showing a steering wheel while driving a car (Abstraction — only necessary controls) and keeping engine parts private (Encapsulation — internal details protected) — both work together, but serve different purposes.

AbstractionEncapsulation
FocusWhat to show (design)How to protect it (implementation)
Achieved viaabstract class, interfaceprivate fields + getter/setter
ExampleA car's steering — internals hiddenAccount.balance being private