Why can an abstract class have a constructor? As we can't create an object of it why would we need a constructor in an abstract class?
9 Answers
In some cases we need to initialize the fields in the abstract class. If it is a empty constructor this is done implicit by the constructor in the child class, otherwise we use super(parameters)
. A constructor with parameters forces the child class to specify some parameters (not necessarily from its own parameters).
All in all, this means that the constructor is used by the child class constructor and not from the "outside".

- 17,622
- 5
- 63
- 99
By adding a constructor to an abstract class you can force the child classes to make a call to super in order to initialize some fields. Example:
abstract class Person {
private String name;
public Person(String name) {
this.name = name;
}
}
class SoccerPlayer extends Person {
public SoccerPlayer(String name) {
super(name);
}
}
Person p = new SoccerPlayer("Ronaldo");

- 11,098
- 6
- 39
- 57
-
3
-
@marcosbeirigo `force the child classes to make a call to super` -- Wouldn’t you have to also add a private no-arg constructor to accomplish the goal of forcing the subclass to call the abstract superclass’ constructor with arguments? – Basil Bourque Dec 01 '14 at 08:35
You still need to be able to create a sub-class which must call its parent's constructor and its parents etc.
The real question is why can you make an abstract classes constructor public. ;)

- 525,659
- 79
- 751
- 1,130
because an class that inherits your abstact class can call this constructor
abstract class Foo {
private int number;
public Foo(int i) {
this.number = i;
}
}
class Bar extends Foo {
public Bar() {
super(1);
}
}

- 12,542
- 4
- 61
- 80

- 6,997
- 2
- 27
- 34
Subclasses can be instantiated and can call the abstract class's constructor from its constructor. Example:
abstract class Foo
{
public Foo()
{
// Do Stuff
}
}
class Bar extends Foo
{
public Bar()
{
super();
}
}

- 4,722
- 4
- 38
- 63
Think of an abstract class like a blueprint of a derived one up to 1 or 2 methods that must be implemented in derived classes. It makes sense to implement as much as possible/sensible in the abstract class, including the constructor.

- 36,037
- 5
- 53
- 100
The abstract constructor forces classes that inherit from the class to implement (or call) the constructor. This implies that you can be sure that any operations in the constructor of the abstract class will be executed.

- 51
- 9
Abstract class represents a more top level of objects, in some cases these top level objects need values to be set at the creation of the object by the business perspective. Missing to set this value could cause business object to fail. The constructors are supported in the abstract class to enforce setting a value when building the class (otherwise it could be forgotten). For example,
public abstract class AccountHolder
{
}
public abstract class Account
{
private AccountHolder holder;
public Account(AccountHolder holder)
{
this.holder = holder;
}
}
public class FixedDeposit extends Account
{
public FixedDeposit (AccountHolder holder)
{
super(holder)
}
}
Here we cannot imagine a account without a holder, so it's essential to the business to set the holder when creating the object. Setting it in the base level ensures it will be forced to set when adding new types of sub classes. Hence it's to ensure 'Open for extensions but closed for modification' one of the SOLID principles.

- 5,974
- 1
- 32
- 43
Constructors in baseclasses can be used to do constructor chaining, so you can init your base class fields for example. In this constructor you can put some businesslogic to validate the parameters
example contructor chainging:
public abstract class Person
{
protected readonly string Name;
protected Person(string name)
{
//Do some validations or regex on the name for example so you know all person names match your business rules
Name = name;
}
}
public class Employee : Person
{
private decimal _salary;
public Employee(string name, decimal salary)
: base(name)
{
_salary = salary;
}
}

- 4,817
- 5
- 34
- 75