BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Abstract classes

An abstract class is an unfinished class: it can hold shared code and state, and declares abstract methods that every concrete subclass must fill in.

Learn these first

At a glance

Mental model

A form template with the header and the footer already printed, and blank boxes in the middle that each department must fill in.

What happens, in order
  1. 1Mark the class abstractMethods marked abstract have no body (abstract double area();), and every non-abstract subclass has to implement them.
  2. 2An abstract class may also have normal methods, fields and constructorsSubclass constructors call the abstract class constructor with super(...).
  3. 3The template method pattern builds on this: the abstract class has a final method that runs the fixed steps and calls the abstract methods for the parts that vary
  4. 4Difference to an interface: a class extends only one abstract class but can implement many interfaces; an abstract class can hold state, an interface cannot (only constants)
Remember
Watch out
Putting abstract on a class and then trying new Shape()Adding state to a base class that later subclasses cannot share cleanlyCalling an abstract method from the abstract class constructor, before the subclass has set its fields
Example (highlighted lines explain it)
public abstract class Report {  public final String render() { return header() + body() + "\n--end--"; }  protected String header() { return "REPORT\n"; }  protected abstract String body();}class SalesReport extends Report {  @Override protected String body() { return "sales: 42"; }}
Read the full explanation (about 1 min)

An abstract class is an unfinished class: it can hold shared code and state, and declares abstract methods that every concrete subclass must fill in. You cannot create one with new.

A mental model

A form template with the header and the footer already printed, and blank boxes in the middle that each department must fill in.

How it works

Mark the class abstract. Methods marked abstract have no body (abstract double area();), and every non-abstract subclass has to implement them.

An abstract class may also have normal methods, fields and constructors. Subclass constructors call the abstract class constructor with super(...).

The template method pattern builds on this: the abstract class has a final method that runs the fixed steps and calls the abstract methods for the parts that vary.

Difference to an interface: a class extends only one abstract class but can implement many interfaces; an abstract class can hold state, an interface cannot (only constants).

Going deeper

Choose an interface for "can do" (Comparable, Runnable) and an abstract class for a shared skeleton with state (AbstractList). Often you offer both: the interface for callers and an abstract base for convenience.

An abstract class can implement an interface without implementing all its methods; the leaf classes do the rest.

Under the hood

The abstract flag on the class file makes new fail at link time with InstantiationError. Abstract methods get a vtable slot without code; the concrete subclass fills it, so calls through the parent type still dispatch dynamically.

Common mistakes

  • Putting abstract on a class and then trying new Shape().
  • Adding state to a base class that later subclasses cannot share cleanly.
  • Calling an abstract method from the abstract class constructor, before the subclass has set its fields.

Part of a learning path

Related concepts

Frequently asked questions

What is Abstract classes?

An abstract class is an unfinished class: it can hold shared code and state, and declares abstract methods that every concrete subclass must fill in. You cannot create one with new.

What are the most common mistakes with Abstract classes?

Putting abstract on a class and then trying new Shape(). Adding state to a base class that later subclasses cannot share cleanly. Calling an abstract method from the abstract class constructor, before the subclass has set its fields.

How can I see Abstract classes in real code?

Paste your own Java into Unravel and it will point out where abstract classes appears, explain how it works at the detail level you choose, and draw the flow of the code around it.