Java Abstract Class

Summary: in this tutorial, you will learn about the Java abstract class and how to use it as a template for other classes.

Introduction to Java abstract class

In Java, an abstract class is a class that cannot be instantiated directly. An abstract class serves as a base class for other classes. In other words, an abstract class defines common behaviors that multiple related classes can inherit.

To define an abstract class, you use the abstract keyword:

abstract class AbstractClassName {
    // ...
}Code language: Java (java)

An abstract method is a method declared without implementation like this:

abstract class AbstractClassName {
    // ...
    abstract dataType abstractMethod(parameters);
}Code language: Java (java)

An abstract class may or may not include abstract methods. But if a class includes an abstract method, it must be an abstract class.

Typically, an abstract class includes one or more abstract methods. In this case, all subclasses of the abstract class need to provide implementations for all abstract methods of the abstract class. If the subclasses do not, they must be marked as abstract classes.

To create a subclass that extends an abstract class, you use the extends keyword:

class MyConcreteClass extends AbstractClassName {
    dataType abstractMethod(parameters) {
        // ...
    }
}Code language: Java (java)

When a subclass extends an abstract class and provides implementations for all the abstract methods, the subclass is called a concrete subclass.

Java abstract class example

Let’s take an example of using a Java abstract class.

First, define the Shape abstract class:

abstract class Shape {
    abstract double area();
    abstract double perimeter();
}Code language: Java (java)

The Shape class has two abstract methods:

  • area() – return the area of the shape.
  • perimeter() – return the perimeter of the shape.

Second, define the Square class that extends the Shape abstract class:

public class Square extends Shape {
    private final double length;

    public Square(double length) {
        this.length = length;
    }

    @Override
    double area() {
        return this.length * this.length;
    }

    @Override
    double perimeter() {
        return 4 * this.length;
    }
}Code language: Java (java)

The Square class is a concrete subclass of the Shape class because it is a subclass of the Shape class and provides concrete implementations for both abstract methods area() and perimeter().

Third, define the Circle class that inherits from the Shape class:

public class Circle extends Shape {
    private final double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double area() {
        return Math.PI * Math.pow(radius, 2);
    }

    @Override
    double perimeter() {
        return 2 * Math.PI * radius;
    }
}Code language: Java (java)

Like the Square class, the Circle class is also a concrete subclass of the Shape class. It implements the area() and perimeter() abstract methods.

Finally, define an array of shapes that contains the instances of the Circle and Square class, iterate the shapes in the array, and calculate the total area and perimeter of all the shapes:

public class App {
    public static void main(String[] args) {
        Shape[] shapes = {
                new Circle(10),
                new Square(20),
        };

        double totalArea = 0;
        double totalPerimeter = 0;

        for (var shape : shapes) {
            totalArea += shape.area();
            totalPerimeter += shape.perimeter();
        }

        System.out.printf("Total area: %.2f\n", totalArea);
        System.out.printf("Total perimeter %.2f\n", totalPerimeter);
    }
}Code language: Java (java)

Output:

Total area: 714.16
Total perimeter 142.83Code language: Java (java)

Summary

  • An abstract class cannot be instantiated and is used as a template for other classes.
  • An abstract method is a method without an implementation.
  • An abstract class may or may not include an abstract method. If a class has at least one abstract method, it must be an abstract class.
  • Use the abstract keyword to define abstract classes and abstract methods.
  • Subclasses that extend an abstract class must provide concrete implementations for all abstract methods of the abstract class, or they must be marked as abstract classes.
  • Abstract classes ensure subclasses implement required behaviors.
Was this tutorial helpful ?