Abstract classes

An abstract class is a class that cannot be created and that may leave some methods without a body. It is the base of a hierarchy whose subclasses must supply the missing parts. A method declared abstract has no body, and every concrete subclass has to override it.

An abstract class

The Shape below has no area of its own, so its area() is abstract. A subclass either implements it or is abstract too. The class is the place for the code that every shape shares, and the abstract method is the part that differs.

The file AbstractShape.java:

public class AbstractShape { public static void main(String[] args) { Shape[] shapes = {new Circle(2), new Rectangle(3, 4)}; for (Shape shape : shapes) { System.out.println(shape.describe()); } } } abstract class Shape { abstract double area(); String describe() { return getClass().getSimpleName() + " with area " + area(); } } class Circle extends Shape { private final double radius; Circle(double radius) { this.radius = radius; } @Override double area() { return Math.PI * radius * radius; } } class Rectangle extends Shape { private final double width; private final double height; Rectangle(double width, double height) { this.width = width; this.height = height; } @Override double area() { return width * height; } }

javac -Xlint:all AbstractShape.java
java AbstractShape

Circle with area 12.566370614359172
Rectangle with area 12.0

describe is concrete: it is written once, it calls the abstract area, and the subclass supplies the answer. That division is the reason the class exists.

The template method

describe is a template method: a concrete method that fixes the order of the steps and calls the abstract ones, which the subclasses implement. A subclass cannot change the order of describe without overriding it, and it does not need to: it supplies area and inherits the report.

The pattern is the usual shape of a framework's extension point. The framework calls the template, the user writes the abstract method, and the control stays with the framework. It is also the reason an abstract class sometimes beats an interface: it can hold the shared code.

The class that cannot be created

new Shape() is a compile error, with the reason in the message. An abstract class has no complete set of methods, so an object of it could not answer every call.

public class NotAbstract { public static void main(String[] args) { Shape shape = new Shape(); System.out.println(shape.area()); } } abstract class Shape { abstract double area(); }

javac -Xlint:all NotAbstract.java

NotAbstract.java:3: error: Shape is abstract; cannot be instantiated
Shape shape = new Shape();
^
1 error

A subclass that does not implement the abstract method is refused in the same way, with a message that names the missing method. The class has to be declared abstract itself, or the compiler reports the abstract method it still carries.

Abstract class or interface

An abstract class can hold fields, constructors and shared code, and a class can extend only one of them. An interface declares methods and can hold constants and default methods, and a class can implement several. The rule of lesson 045 applies: use an abstract class when the subtypes share state or a partial implementation, and an interface when they share only a contract.

The next lesson writes the same shapes as an interface, and lesson 063 compares the two versions side by side.

Exercises

  1. Add a perimeter() abstract method and implement it in both shapes.
  2. Add a concrete method isBiggerThan(Shape other) to the abstract class and use it in main.
  3. Make Circle abstract and see what the compiler says about new Circle(2).
  4. Remove the @Override from one area method and misspell it. What does the compiler report?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is an abstract method?

    Answer

    A method with no body that a subclass must implement

    The declaration ends at the semicolon, and the concrete subclass supplies the body.

  2. Can an abstract class be instantiated?

    Answer

    No, the compiler refuses new on it

    An object of an abstract class could not answer every call, so new is a compile error.

  3. What is a template method?

    Answer

    A concrete method that calls abstract methods a subclass supplies

    It fixes the order of the steps and leaves the variable parts abstract.

  4. What does this program print? public class Q1 { public static void main(String[] args) { Shape shape = new Square(); System.out.println(shape.area()); } } abstract class Shape { abstract int area(); } class Square extends Shape { @Override int area() { return 16; } }

    Answer

    16

    The square implements the abstract method, and the call through Shape runs it.

  5. What does this program print? public class Q2 { public static void main(String[] args) { Shape shape = new Square(3); System.out.println(shape.describe()); } } abstract class Shape { abstract int area(); String describe() { return "area " + area(); } } class Square extends Shape { private final int side; Square(int side) { this.side = side; } @Override int area() { return side * side; } }

    Answer

    area 9

    The template method calls area, which returns 3 times 3.

  6. What is the first line of the compiler output for this program? public class Q3 { public static void main(String[] args) { Shape shape = new Shape(); } } abstract class Shape { abstract int area(); }

    Answer

    error: Shape is abstract; cannot be instantiated

    An abstract class has no complete set of methods, so the compiler refuses the creation.

  7. What happens when a concrete subclass leaves an abstract method unimplemented?

    Answer

    The compiler refuses the subclass

    A concrete class must implement every abstract method it inherits, or be declared abstract itself.

  8. What does this program print? public class Q4 { public static void main(String[] args) { Shape[] shapes = {new A(), new B()}; for (Shape shape : shapes) { System.out.print(shape.id()); } System.out.println(); } } abstract class Shape { abstract String id(); } class A extends Shape { @Override String id() { return "a"; } } class B extends Shape { @Override String id() { return "b"; } }

    Answer

    ab

    The array holds one of each, and print writes them in order on one line.

  9. Which member can an abstract class hold that an interface cannot?

    Answer

    An instance field

    Interfaces have no instance state; an abstract class can have fields and constructors.

  10. Why does describe() not need to be overridden by every shape?

    Answer

    It is concrete and uses the abstract area each shape supplies

    The shared code is written once in the abstract class and calls the method that differs.

The code on this page was built with OpenJDK 21.0.13 (javac and java) and run on Alpine Linux in a Docker container.

Article author: Arthur Isaev