EMZETT.
Login

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

FeatureAbstract classInterface
Fields with stateyesno (constants only)
Constructoryesno
Multiple inheritancenoyes (many interfaces)
Methods with codeyessince Java 8 as default
Relationship”is a""can do”
  • Choose an abstract class: when related classes share code and state (Animal with a name and eat()).
  • 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