Can an abstract class have a constructor? Why would it need one?
Yes. It cannot be instantiated directly, but a subclass constructor calls it with super() so the parent's fields get initialised.
Explain it simply
An abstract class is a half-built foundation. You never move into it, but every house built on it still needs the foundation laid properly. The constructor does that setup work when a concrete child object is created.
Example
abstract class Vehicle {
protected final String regNo;
Vehicle(String regNo) { // legal and common
this.regNo = regNo;
System.out.println("Vehicle ready: " + regNo);
}
abstract int wheels();
}
class Auto extends Vehicle {
Auto(String regNo) { super(regNo); } // runs the abstract class constructor
int wheels() { return 3; }
}
new Auto("JH05-1234"); // prints: Vehicle ready: JH05-1234Calling an overridable method from an abstract class constructor is risky: the child's fields are not yet set at that moment.
What to say in the interview
"Yes, abstract classes have constructors, used through super() to initialise shared fields. The class cannot be instantiated on its own, but its constructor runs whenever a concrete subclass object is built."
Common mistakes
- Saying abstract classes cannot have constructors because they cannot be instantiated.