Abstract class or interface?
In short: An abstract class is an unfinished blueprint with shared state and code. An interface is a pure contract: it says what a class must be able to do, not how.
In more detail: Neither can be instantiated directly. A class inherits from at most one abstract class but can implement many interfaces. Modern interfaces may contain default methods but no object state.
In detail
| Feature | Abstract class | Interface |
|---|---|---|
| Fields with state | yes | no (constants only) |
| Constructor | yes | no |
| Multiple inheritance | no | yes (many interfaces) |
| Methods with code | yes | since Java 8 as default |
| Relationship | ”is a" | "can do” |
- Choose an abstract class: when related classes share code and state (
Animalwith a name andeat()). - Choose an interface: when different classes share the same ability (
Comparable,Runnable). - Example:
abstract class Shape { abstract double area(); },interface Drawable { void draw(); }. - Combination: often both: interface as contract, abstract class as a skeleton.
See also: Abstraction, Inheritance