Subclassing Abstract Classes
What you'll learn: How to extend an abstract class and fulfill the contract of implementing its abstract methods.
The Core Idea
When you create a concrete subclass (one you can actually instantiate) that extends an abstract class, Java enforces a simple rule: you must implement every abstract method inherited from the parent. This is the "contract" that abstract classes establish—they define what must be done, and subclasses define how to do it.
Think of it like a job posting: the abstract class lists the required responsibilities (abstract methods), and the concrete subclass is the employee who must actually perform all those tasks.
How It Works
When you use extends with an abstract class:
abstract class Shape {
abstract double area(); // No body—subclasses must provide
void display() { // Concrete method—inherited as-is
System.out.println("This is a shape");
}
}
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
@Override
double area() { // MUST implement this
return Math.PI * radius * radius;
}
}
If Circle didn't implement area(), the compiler would reject it. You'd have two choices: implement the method, or declare Circle itself as abstract.
The Enforcement
Java's compiler checks at compile-time that all abstract methods are implemented. This guarantees that any object you can actually create (instantiate) has complete, working implementations of all required behavior.
You inherit concrete methods normally—no need to override them unless you want to.
Key Takeaway: A concrete subclass of an abstract class must implement all inherited abstract methods, or it too must be declared abstract. This contract ensures complete, instantiable objects.