Syllabus / Java / Phase 3: Object-Oriented Programming (Days 11–16)
Intermediate

Abstraction

📂 Phase 3: Object-Oriented Programming (Days 11–16) · Java

Abstraction means hiding complex implementation details and exposing only the essential features needed to use an object — focusing on "what" something does rather than "how" it does it.

Abstract Classes

An abstract class can have both regular methods and abstract methods (declared without a body). It cannot be instantiated directly — only subclassed.

abstract class Shape {
    abstract double area(); // no body — subclasses MUST implement this

    void describe() { // regular method, shared by all subclasses
        System.out.println("This shape has an area of " + area());
    }
}

class Circle extends Shape {
    double radius;
    Circle(double radius) { this.radius = radius; }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}

Circle c = new Circle(5);
c.describe(); // This shape has an area of 78.53...

Key Rules for Abstract Classes

  • Cannot be instantiated: new Shape() would not compile
  • Any class extending an abstract class with abstract methods MUST override all of them, or also be declared abstract
  • Can contain constructors, fields, and fully implemented methods alongside abstract ones

Real-World Analogy

When you press the accelerator in a car, you don't need to know how fuel injection, combustion, or the transmission work internally — you only interact with the simple "accelerate" interface. The complexity is abstracted away.

Partial vs Complete Abstraction

MechanismAbstraction Level
Abstract classPartial — can mix abstract and concrete methods
InterfaceComplete — pure contract (covered next, Day 16)
Interview tip: "What's the difference between abstraction and encapsulation?" — Abstraction hides implementation complexity (what vs how); encapsulation hides internal data via access control (private fields + public methods). They're complementary, not the same thing.