Lesson · Abstraction & Interfaces
Abstract classes: a parent that works in promises
15 min readAbstract Classes
An abstract method is a promise with no body; an abstract class is a parent that cannot be created until every promise is kept. The template method pattern is where the idea earns its keep.
The base class that lies
A concrete base class has to pick some answer for every method. Watch what that answer becomes:
class Shape { double area() { return 0; } // the area of... nothing? } Shape s = new Shape(); // compiles, and lies System.out.println(s.area()); // 0.0, in silence
Predict: which is worse for a caller: a compile error, or this silent 0.0? The base class does not know what an area is; it filled the blank with a wrong answer. An abstract method removes the blank entirely: no body, and no class object until a subclass supplies one.
What abstract means, piece by piece
Three words carry the whole idea:
| Piece | Means |
|---|---|
| abstract method | A method declared with no body: the parent promises the method exists and leaves the answer to subclasses |
| abstract class | A class that cannot be instantiated with new; it may mix abstract promises with fully implemented methods, fields and constructors |
| concrete subclass | A subclass that implements every inherited promise; then, and only then, new is allowed |
- Constructors still exist in abstract classes: an abstract parent's constructor runs when a subclass is created (through
super(...)), because parent state still needs building. - A concrete class must answer everything. Leave one abstract method unimplemented and the subclass itself must be abstract; the compiler refuses halfway houses.
- `new Shape()` is a compile error, not a runtime surprise:
Shape is abstract; cannot be instantiated. The language catches the lie before the program runs.
The rule of the promise: the parent fixes what must be true, the subclass supplies the how. Until every how is supplied, no object exists.
The template method: parent owns the sequence
The pattern that makes abstract classes shine: the parent writes the steps once, in order, and leaves the variable parts as abstract methods (called hooks):
abstract class Report { final void generate() { // the template: sequence is fixed System.out.println("== " + title() + " =="); System.out.println(body()); } abstract String title(); abstract String body(); }
generate()is the template: the parent decides the sequence and cannot be replaced (final).title()andbody()are the promises; each report supplies its own text.- A subclass only answers the blanks; it cannot reorder or forget the frame.
- Calling
generate()on a SalesReport prints its title and body; a DraftReport prints its own. One sequence, many contents.
Inheritance upside down, on purpose. Instead of subclasses calling the parent for help, the parent calls subclasses for answers: the famous Hollywood principle, 'do not call us, we will call you'.
A parent that works in promises
Current statement
The classes
Shape · abstract
Shape is half a class: one promise (area, no body) and one finished method (describe).
The parent states what must be true: every shape has an area, and every shape can describe itself. The describe() method is written once, in full, using the promise.
Abstract, concrete, or interface?
Picking the tool for a base type:
| You have | Reach for |
|---|---|
| Shared code plus steps that must vary | Abstract class with concrete methods and abstract hooks |
| A pure contract for unrelated classes | Interface (the next lesson) |
| Everything already known | A plain concrete class |
| Optional tuning points for subclasses | Concrete class with overridable (non-final) methods |
Abstract is for gaps, not for secrets. If every method can be implemented today, the class should be concrete; reserve abstract methods for questions only subclasses can answer.
The abstract class playbook
Building a parent that works in promises, in order:
- Write the common code as concrete methods first; the blanks will reveal themselves.
- Mark the blanks
abstract: each must be something only a subclass knows (a title, an area, a cost formula). - Keep the sequence of a public workflow in one
finaltemplate method so subclasses cannot break the order. - Give the class a constructor if it has state; remember it still runs, through super, for every subclass.
- Test with two subclasses: one typical, one minimal. If the minimal one cannot answer a hook, the hook is not minimal enough.
A promise with no body, kept by others. That sentence is the whole keyword: the parent guarantees the method is never missing, and refuses to exist until someone supplies the body.
Abstract mixes: promises the parent makes, answers the children bring
An abstract method has no body and every concrete subclass must supply one; an abstract class can still hold state, constructors and finished methods, and it cannot be instantiated until all promises are kept. The template method pattern is the flagship use: the parent fixes the sequence with final, subclasses fill the steps.
Interview angles
Why can an abstract class not be instantiated?+
Because its promises are not answered yet: at least one method has no body, so an object would not know how to behave. The compiler refuses new with Shape is abstract; cannot be instantiated.
The refusal is a feature: it makes 'an unanswerable object' impossible to create, so callers never meet the silent 0.0 of a lying default. Constructors still exist and still run for subclasses, through the usual super chain.
abstract class Shape { abstract double area(); } new Shape(); // compile error, before anything runs
Common misconception: That abstract classes are 'interfaces with extra steps'. They carry state, constructors and concrete behaviour; interfaces carry none of that.
Follow-up: Where does the parent's constructor run, if the parent has no objects?
What must a concrete subclass of an abstract class do?+
Implement every inherited abstract method. If even one remains unanswered, the subclass must itself be abstract, or the compiler refuses it.
The error names the exact gap and its owner: Circle is not abstract and does not override abstract method area() in Shape. There is no partial credit: a class either keeps every promise or admits it is unfinished.
abstract class Shape { abstract double area(); } class Circle extends Shape { } // error: must implement area() abstract class Blob extends Shape { } // fine: still unfinished, still abstract
Common misconception: That the compiler will pick something. There is no default body to fall back to; the gap is the point.
Follow-up: Why would you deliberately create an abstract subclass with no new methods?
Explain the template method pattern with an example.+
A parent method fixes a sequence of steps and calls abstract hooks for the parts that vary; subclasses supply only those parts and cannot change the frame.
Report.generate() prints a header from title() and a body from body(): one sequence, many reports. The template method is usually final so no subclass can reorder the workflow; the hooks are protected so they are exactly the subclass interface.
final void generate() { System.out.println(title()); System.out.println(body()); } abstract String title(); abstract String body();
Common misconception: That the pattern is about sharing code. It is about sharing a *sequence*: the order of operations is the invariant, and the hooks are the variation points.
Follow-up: Why should a template method often be final?
Can an abstract class have fields and constructors?+
Yes. That is its advantage over interfaces: it can hold instance state, initialise it in constructors, and every subclass object carries it.
When a subclass is created, the abstract parent's constructor runs through the super chain like any other. Common initialisation then lives in one place, and subclasses see the fields through protected access or getters.
abstract class HttpJob { protected final String url; // shared state HttpJob(String url) { this.url = url; } abstract void perform(); }
Common misconception: That abstract classes avoid state to stay flexible. Flexibility comes from the unanswered methods; the answered state is shared machinery.
Follow-up: When does state in a base class become a liability?
Abstract class or interface: what tips the decision?+
State, constructors and partial implementation point to an abstract class; unrelated classes sharing a capability, and multiple inheritance of type, point to an interface.
Many APIs use both together: the interface defines the contract (for example List), an abstract class implements the boring parts (AbstractList), and implementers pick whichever fits: implement the interface directly, or extend the skeleton and fill a few gaps.
class MyList<E> extends AbstractList<E> { // skeleton does the drudgery public E get(int i) { ... } public int size() { ... } }
Common misconception: That the two compete. They compose: the contract for callers, the skeleton for implementers.
Follow-up: What does AbstractList leave for you to implement?