Abstract classes
An abstract class is a class you cannot instantiate. It holds shared fields and methods, and it can leave some methods unimplemented for subclasses to fill in.
Detailed Explanation
Sometimes several types share a skeleton and differ in one step. A report always has a title and a render method, but the body changes. An abstract class stores the title, implements render, and marks body() abstract. Subclasses become small. Callers still cannot write new Report(...), because a report without a body is incomplete.
Abstract methods have no body. The class that contains even one must be declared abstract. Abstract classes may also have ordinary methods, constructors, fields, and static members. Constructors run when a subclass is created. That is how you force every child to pass a name upward.
Choose an abstract class over an interface when you need protected fields, a constructor that establishes invariants, or a large amount of shared implementation that is awkward as default methods. Choose an interface when types do not share lineage, or when a class already has a superclass. A common pattern is both: the interface is the published type, and an abstract class is an optional helper for implementors (AbstractList behind List).
Template method is the design that falls out naturally. A final method on the abstract class calls abstract hooks in a fixed order. Subclasses cannot break the order, only supply the hooks. That is stronger than an interface default, because you can hold state and hide the hooks as protected.
You reach for an abstract class when the shared code is real, not speculative. Three subclasses copying the same validation is a signal. You avoid it when the shared code is a single utility function: a static method on a final class is enough, and it does not consume the one superclass slot.
The cost is the same coupling as any inheritance. The abstract class becomes a bottleneck. Changing a protected field or the template order breaks children. Deep abstract stacks (AbstractFoo extends AbstractBar) are hard to debug. Keep the abstract layer thin, keep hooks few, and prefer interfaces for the type that appears in public method signatures.
Code Examples
abstract class Job {
private final String name;
protected Job(String name) {
this.name = name; // every subclass must pass a name upward
}
// Template: subclasses cannot skip logging.
public final void run() {
System.out.println("start " + name);
execute();
System.out.println("end " + name);
}
protected abstract void execute();
}
class PingJob extends Job {
PingJob() {
super("ping");
}
@Override
protected void execute() {
System.out.println("pong");
}
public static void main(String[] args) {
new PingJob().run();
// prints:
// start ping
// pong
// end ping
}
}abstract class Shape {
abstract int area();
int twice() {
return area() * 2; // shared code that depends on the abstract method
}
}
class UnitSquare extends Shape {
@Override
int area() {
return 1;
}
public static void main(String[] args) {
System.out.println(new UnitSquare().twice()); // prints: 2
}
}Common Gotchas
`new` on an abstract class does not compile
even if every method has a body, `abstract` blocks instantiation. Use a concrete subclass or an anonymous class.
Abstract classes consume the single superclass
a type that already extends something else cannot also extend your helper. Offer an interface plus a static helper.
Calling abstract methods from the abstract constructor is unsafe
the subclass fields are not initialized yet.
A class with no abstract methods can still be abstract
use that to block `new` on a utility base you never want constructed.