Course contentsShow
← Java
Lesson 484 of 2,87012. Abstract Classes and InterfacesFree lesson

Constructors in Abstract Classes

Defining constructors in abstract classes that can be invoked via super() from subclass constructors despite no direct instantiation.

Constructors in Abstract Classes

What You'll Learn

You'll learn how abstract classes can—and should—define constructors to initialize their fields, even though these classes cannot be instantiated directly.

The Core Idea

Even though you can't create an instance of an abstract class with new AbstractClass(), abstract classes can and should have constructors. Why? Because when you instantiate a concrete subclass, the abstract superclass's constructor runs automatically via the constructor chaining process you've already learned.

Think of it like this: an abstract class is a blueprint that defines shared state and behavior. Even though you can't build a "generic vehicle," every specific vehicle (car, bike, truck) needs the foundational parts that the vehicle blueprint defines—engine size, number of wheels, etc. The abstract class constructor initializes those shared parts.

How It Works

When a subclass constructor runs, it invokes super() (either explicitly or implicitly), which calls the abstract class's constructor. This allows the abstract class to:

  • Initialize its own fields
  • Enforce initialization rules for all subclasses
  • Set up common state before the subclass completes its own initialization
abstract class Vehicle {
    protected String brand;
    
    // Constructor in abstract class
    public Vehicle(String brand) {
        this.brand = brand;
    }
}

class Car extends Vehicle {
    private int doors;
    
    public Car(String brand, int doors) {
        super(brand);  // Calls Vehicle's constructor
        this.doors = doors;
    }
}

Even though new Vehicle("Toyota") would fail, the Vehicle constructor runs every time you create a Car.

When to Use Them

Define constructors in abstract classes whenever the abstract class has fields that need initialization. This centralizes shared initialization logic and ensures all subclasses properly initialize inherited state.

Key Takeaway: Abstract classes can and should have constructors to initialize their fields; these constructors are invoked through super() during subclass instantiation, ensuring proper initialization of inherited state.