SteelReed

Search

Search posts, questions…

OOP & DesignQuestion 6 / 42

Junior

Abstract class vs interface: what is the difference and when do you use each?

Short answer

An interface is a contract ("what it can do"); a class can implement many. An abstract class is an unfinished parent ("what it is"): it has state and constructors, but you can extend only one. Default to an interface.

NoYesSubclasses sharestate, constructor?InterfaceAbstract classimplement manyextend only one

3 points to say:

  1. Interface: implement many, no instance state.
  2. Abstract class: state, constructors, extend only one.
  3. Clashing default methods: override and pick with A.super.hi().

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.