AlgoViz

Interface vs abstract class

medium 45 min

An interface says what something can do and leaves the how to others.

Watch lesson4:42

Where it shows up

The same pattern appears across everyday software.

  • Java List, AbstractList, ArrayList

    The documentation calls AbstractList a skeletal implementation of the List interface that minimizes the effort to implement it. Callers hold List.

  • Java Comparable, Iterable, AutoCloseable

    Three promises that unrelated classes make: can be ordered, can be looped over, can be closed by try-with-resources.

  • Java InputStream

    An abstract class. A subclass must always provide the method that returns the next byte. The other read methods have bodies that can use it.

  • Jakarta HttpServlet

    Documented as an abstract class to be subclassed. A subclass must override at least one method, usually doGet or doPost, although none of them is declared abstract.

  • Java Serializable and Cloneable

    Markers. Serializable has no methods or fields. Cloneable does not contain the clone method.

  • Python collections.abc

    Implement the few abstract methods of Sequence and the mixin methods such as index and count come with it. One tool for both ideas.

  • Rust Iterator

    A trait with one required method, next, and a long list of provided methods such as map and filter that are built on it.

  • .NET Stream

    The abstract base class of all streams. Files, memory and network streams derive from it.

  • Go interfaces

    The Go FAQ explains why there is no implements declaration: a type implements an interface by implementing its methods, nothing more.

  • React class components

    Component is the base class for components written as classes. In JavaScript it is not declared abstract, and React now recommends functions for new code.