Why this matters
- This is the most frequently asked design question in Java interviews, and default methods in Java 8 changed the correct answer.
- Choosing wrongly shows up later as a class that cannot extend the thing it needs to, because its one
extendsslot is already taken. - Every modern Java API — streams, collections, Spring, JDBC — is defined in interfaces, so reading those APIs requires fluency here.
Interfaces
An interface is a contract. Before Java 8 it could hold nothing but abstract method signatures and constants; three releases have since widened it.
Member rules, stated once
- Methods without a body are implicitly
public abstract. - Fields are implicitly
public static final— constants, never instance state. defaultmethods carry a body that implementers inherit and may override.staticmethods belong to the interface itself and are not inherited by implementers.privatemethods exist only to share code betweendefaultmethods in the same interface.- An interface has no constructor and can never be instantiated.
Abstract classes
An abstract class cannot be instantiated and may mix finished and unfinished methods. Crucially, it can hold state and a constructor.
Note what describe() does: it calls the abstract area(). The parent defines the algorithm and
the child supplies one step. That is the template method pattern, and it is the clearest reason to
prefer an abstract class.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 5 sections
- Choosing between them
- When two interfaces collide
- Functional interfaces
- Marker interfaces and annotations
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.