Concept · Core

Abstract class vs Interface

An abstract class is a half-built base you EXTEND (one parent, shared code + state); an interface is a pure contract you IMPLEMENT (many of them, no state). Knowing which to reach for is a classic interview question.

Asked atAmazonMicrosoftOracleAdobe
step 1 / 5
Extend one, implement many
«abstract»
Game
# players: int
+ play()
# initialize()*
# endGame()*
«interface»
Savable
+ save()
Chess
# initialize()
# endGame()
+ save()
«interface»
Rankable
+ rank()
Extend one, implement many
1abstract class Game {
2 protected int players;
3 public final void play() { initialize(); endGame(); }
4 protected abstract void initialize();
5 protected abstract void endGame();
6}
7interface Savable { void save(); }
8interface Rankable { int rank(); }
9 
10class Chess extends Game implements Savable, Rankable { ... }
State
abstractcannot be new-ed
state +concrete + abstract

Game is an ABSTRACT class: it holds state (# players), a CONCRETE template method play(), and ABSTRACT methods (marked *) that subclasses must fill in. You can never `new` it directly.