Lesson · Abstraction & Interfaces
Interface or abstract class? A decision you can defend
15 min readInterface vs Abstract Class
Interfaces carry capabilities across any families; abstract classes carry shared state and half-finished code. The mature answer is often both: the contract for callers, a skeleton for implementers.
Two shapes of problem
One requirement reads: 'orders and subscriptions must both be refundable'. Another reads: 'every report shares a header field and the same print sequence, but writes its own body'. Same chapter, different verdicts:
interface Refundable { // capability: any class, any family void refund(); } abstract class Report { // identity with shared machinery protected String header; // state lives here Report(String header) { this.header = header; } void setHeader(String header) { this.header = header; } // common behaviour abstract String body(); // the variation point }
Predict: what breaks if the report logic lives in an interface instead? The state (header) and the constructor have no home: interfaces hold none of either. What breaks if refundability lives in an abstract class? Orders and subscriptions would need a shared parent they otherwise do not have.
The comparison, point by point
Six questions separate the two tools in practice:
| Question | Interface | Abstract class |
|---|---|---|
| The sentence it captures | Can do (a capability) | Is a (an identity) |
| Per class | Any number | Exactly one parent |
| Instance state | None; constants only | Fields, freely |
| Constructors | None; nothing to build | Yes; they run for every subclass through super |
| Method bodies | Signatures, plus default bodies | Abstract and concrete methods mixed freely |
| Reaching across families | Its whole job | Impossible: one parent already spent |
State and constructors are the tie-breakers. If the contract needs either, it is an abstract class (or a composed helper); if it must fit under any parent, it is an interface.
Skeletal implementations: the two tools together
Java's own collections show the mature pattern: List is the interface every caller holds, and AbstractList is an abstract class that implements the boring parts once. A custom list only fills the specifics:
class TwoItems extends AbstractList<String> { @Override public String get(int index) { return index == 0 ? "a" : "b"; } @Override public int size() { return 2; } // iterator(), indexOf(), equals(), toString() arrive free }
- Two methods are genuinely yours:
getandsize, the parts only the storage knows. - Iteration,
indexOf,contains,equalsandtoStringcome from the skeleton, written correctly once. AbstractListitself implementsList, so extending it makes the class a List for every caller.- The same pattern ships as
AbstractCollection,AbstractSet,AbstractMap: use the interface as the type, the skeleton as the head start.
Interfaces and abstract classes are not rivals; they are teammates. One defines what callers may rely on, the other saves implementers from rewriting the plumbing.
Decision board · interface or abstract class
pick a situationThe situation
An Order and a Subscription must both support refund(). The two classes share no parent, and never will.
The requirement is a capability crossing families. An abstract class would force a shared parent and spend the single inheritance slot on a relationship that does not exist.
Name the ability: Refundable.
The decision playbook
Choosing the construct, in order:
- Write the sentence: 'can do' tips to an interface, 'is a' with shared state tips to an abstract class.
- Check the families: must unrelated classes adopt the type? Only an interface can.
- Check the memory: fields, constructors or per-object data? Only an abstract class (or composition) can.
- Expect growth? Interfaces grow safely with default methods; abstract classes grow by adding concrete members, which subclasses inherit automatically.
- Building an API others implement? Ship both: the interface as the type, an abstract skeleton for convenience, exactly as
ListandAbstractListdo.
The interface is the promise; the abstract class is the shortcut. Callers hold the promise, implementers may take the shortcut, and neither choice costs the other anything.
Capability crosses families; identity carries state
An interface is a capability any class can adopt, any number at once, with no state and no constructors. An abstract class is an identity with shared fields, constructors and half-finished work, and it spends the single inheritance slot. When callers need a contract and implementers want a head start, ship both: `List` and `AbstractList` is the pattern language for it.
Interview angles
Interface or abstract class: the shortest version?+
Capability versus identity. An interface says what a class can do and crosses any families; an abstract class says what a class is, with shared state, constructors and partial implementation.
The mechanical differences follow: many interfaces, one parent; no instance state, full state; no constructors, constructors that run through super. Pick by what the contract needs, not by taste.
interface Refundable { void refund(); } // capability abstract class Report { String header; abstract String body(); } // identity + machinery
Common misconception: That abstract classes are 'stronger'. They are narrower: one per class, and they block a class's natural parent.
Follow-up: What does an abstract class give up in return for state?
Can a class extend an abstract class and implement interfaces at the same time?+
Yes, and that is the standard shape: one parent for state and code, any number of interfaces for types. class MyList extends AbstractList<String> is simultaneously a MyList, an AbstractList and a List.
The parent contributes memory and the template; each interface contributes a type callers can rely on. AbstractList itself implements List, so extending it makes the class a List for free, exactly as the collections framework intends.
class TwoItems extends AbstractList<String> { ... } List<String> list = new TwoItems(); // the interface is the type callers hold
Common misconception: That interfaces and the parent compete. They layer: state from one place, types from many.
Follow-up: What must a class still implement when extending AbstractList?
What is a skeletal implementation, and why does the JDK ship them?+
An abstract class that implements an interface's boring parts so concrete classes only fill the specifics: AbstractList needs get and size, and delivers iterator, indexOf, equals and more.
Skeletons encode the tedium once, correctly, in the JDK rather than in thousands of implementations. The cost: you inherit their assumptions (AbstractList assumes indexed access), so a type with different mechanics may implement the interface directly instead.
@Override public String get(int index) { ... } // yours @Override public int size() { ... } // yours // iterator(), indexOf(), equals(): the skeleton's
Common misconception: That skeletons are legacy. They are the recommended pattern for any interface expected to have many implementers.
Follow-up: When would you implement List directly instead of extending AbstractList?
When is an abstract class the right home for shared behaviour?+
When subclasses genuinely share state or construction, or when a fixed sequence with variation points (the template method) is the design.
Fields and constructors live nowhere else in a type hierarchy without composition. The trade is the single inheritance slot: pick the abstract parent only when the is-a relationship is real and permanent.
abstract class HttpJob { protected final String url; HttpJob(String url) { this.url = url; } final void run() { connect(); perform(); disconnect(); } abstract void perform(); }
Common misconception: That any code-sharing need justifies an abstract parent. If only a method or two is shared and there is no state, a default method or a static helper serves better.
Follow-up: What happens to the inheritance slot once an abstract parent is chosen?
How do APIs combine the two deliberately?+
They accept the interface in every signature and ship an abstract skeleton for implementers: List and AbstractList, Map and AbstractMap, Set and AbstractSet.
Callers program against the contract and never depend on the skeleton; implementers choose their cost: implement the interface directly for full control, or extend the skeleton for speed. Both remain compatible with every method that took the interface.
void printAll(List<String> items) { ... } // callers: interface class MyList extends AbstractList<String> // implementers: skeleton
Common misconception: That the skeleton is the 'real' type. The interface is what everything is written against; skeletons are replaceable convenience.
Follow-up: Which of the two would you put in a public API's parameter types?