library
beginnerAbstract Classes vs Interfaces
Know exactly when to choose an abstract class over an interface — a top-5 Java interview question.
Both let you declare something without implementing it. The difference is what else they can carry.
Abstract class = partially assembled IKEA shelf. Interface = a blueprint saying 'must have shelves and doors' — build however you want, follow multiple blueprints.
Key Concepts
1
abstract class interface
---------------------------------------------------
instance fields yes no (constants only)
constructors yes no
how many extend 1 implement many
method access any public (or private helpers)
default bodies yes yes, since Java 82
Pick an abstract class when subtypes share state.
abstract class Shape {
protected final String id; // interfaces cannot hold this
Shape(String id) { this.id = id; }
abstract double area();
}3
Pick an interface for a capability that unrelated classes have:
4
interface Comparable<T> { int compareTo(T o); }5
A String and a LocalDate have nothing in common except that both can be ordered. There is no sensible
shared superclass, so this must be an interface.
StringLocalDate
6
Java 8 changed the balance. Interfaces got default methods:
7
interface Logger {
void log(String msg);
default void warn(String msg) { log("WARN: " + msg); }
}8
Before that, any shared helper forced an abstract class. Now most of them do not.
9
Adding to an interface is backward-compatible if you add a default method. Adding an abstract method to
an abstract class breaks every subclass immediately.
10
So the modern rule: interfaces define types, abstract classes share implementation. Reach for the
abstract class only when you need fields, a constructor, or a non-public method.
11
You can use both, and the JDK often does. AbstractList implements List and gives subclasses a head
start — the interface is the type, the abstract class is a convenience.
AbstractListList
12
What the interviewer is probing.1. "When would you choose an abstract class over an interface?" Probing: whether you have a
rule or a preference. Stalls: "Abstract classes are for related things." Moves up: shared
mutable state, a constructor, or non-public methods — an interface cannot carry any of those.
13
2. "Since Java 8 interfaces have default methods. Does that make abstract classes obsolete?"
Probing: whether you know what defaults did and did not change. Stalls: "Yes, interfaces can do
everything now." Moves up: defaults removed the need for an abstract class purely to share a
helper, but interfaces still have no instance fields and no constructors.
14
3. "A class implements two interfaces with the same default method. What happens?" Probing:
the diamond resolution rules. Stalls: "The first one wins." Moves up: it does not compile; the
class must override and can pick with A.super.f().
15
4. "Why can you add a default method to a published interface but not an abstract method?"
Probing: binary compatibility, and why defaults exist at all. Stalls: "Both are fine." *Moves
up:* an abstract method breaks every existing implementation; a default gives them an inherited
body, which is how forEach was added to Collection.