beginnerFree

Abstract Classes

Partial blueprint - some methods, some holes

Abstract Classes

Abstract classes define a contract with some implementation. Subclasses fill in the rest.

The narrated version of this skribble — tap play for the full-size video.

0:00 / 2:20

The Big Picture

Abstract classes sit between interfaces and concrete classes. They can mix implemented and abstract methods, but can't be instantiated directly.

  • Use when subclasses share common code
  • Template Method pattern: skeleton with overridable steps
  • Examples: AbstractList, HttpServlet, InputStream

How It Works

Compiler blocks new on abstract classes. Subclasses must implement all abstract methods or be abstract themselves.

  • Abstract methods have no body (just signature)
  • Concrete methods work like regular class methods
  • Can have fields, constructors, static methods
  • Constructors run normally for inheritance chain

In the Java Landscape

Abstract classes bridge interfaces and full inheritance.

  • Java 8+ default methods blur the line with interfaces
  • Single inheritance limit applies (one superclass)
  • Often combined with interfaces for flexibility
  • Common in collection and UI frameworks
  • Sealed abstract classes (Java 17+) restrict hierarchy

Pitfalls and Best Practices

  • Prefer interfaces + composition over abstract classes
  • Use abstract classes when you need state or constructors
  • Don't make everything protected - be selective
  • Document the subclass contract clearly
  • Keep small to avoid fragile base class problem
#java#oop#abstract