Can Abstract Class have a Constructor in Java?

Yes. Abstract classes can also have constructors just like any other normal class. The advantage of declaring a constructor within an abstract class is that it can be called from any subclass constructor using the super keyword to reuse the initialization logic when the subclass is being instantiated.

This way you can put common instantiation code in one place and have the subclasses use it as required.

package com.techstackjournal;

abstract class Employee {
	private int empId;
	private String empName;

	public Employee(int empId, String empName) {
		this.empId = empId;
		this.empName = empName;
	}
}

class Manager extends Employee {
	public Manager(int empId, String empName) {
		super(empId, empName);
	}
}

public class Main {
	public static void main(String[] args) {
		Manager m = new Manager(1, "John Doe");
	}
}

In the above example, Employee is an abstract class, however, it has got a constructor with empId and empName. The Manager subclass, which is a concrete subclass reuses the superclass constructor by calling it using a super keyword at the time of instantiating it.

Why do abstract class have a constructor in Java?

An abstract class have a constructor to provide a common initialization logic for all of its member variables which are shared across multiple subclasses.

The purpose of declaring an abstract class is to provide a set of member variables and methods that are shared across all subclasses. Similarly, the initialization logic of these member variables should also be shared across all the subclasses, so it has to be placed in the abstract class. Since constructors are meant for initializing the member variables, it is the right place to initialize the common member variables within the abstract class.

How an abstract class constructor is called?

As you know that you cannot instantiate an abstract class, you must be wondering how an abstract class constructor will be called. An abstract class constructor is called when its concrete subclass is instantiated.

If an abstract class is having a no-arg constructor, it will be called by subclass implicitly. You can also call abstract class constructor be it no-arg or with-arg, explicitly using the super keyword as we do with normal classes.

Can abstract class have a private constructor?

Yes, an abstract class can have a private constructor. However, no one will be able to use that constructor. Why? first of all, an abstract class itself cannot be instantiated, so whatever constructor you define can be called by its subclasses using super keyword.

But if you declare the constructor as private, that constructor will be invisible to even the subclass of that abstract class. You also cannot call this constructor within the abstract class also This is the reason why abstract class private constructors are not accessible from anywhere.

The only way a private constructor can be executed is by another public constructor within the abstract class using this keyword. A subclass has to call that public constructor of the abstract class using the super keyword.

package com.techstackjournal;

abstract class Employee {
	private int empId;
	private String empName;

	private Employee(int empId) {
		this.empId = empId;
		System.out.println("private constructor executed...");
	}

	public Employee(int empId, String empName) {
		this(empId);
		this.empName = empName;
	}
}

class Manager extends Employee {
	public Manager(int empId, String empName) {
		super(empId, empName);
	}
}

public class Main {

	public static void main(String[] args) {
		Manager m = new Manager(1, "John Doe");
	}

}

As explained earlier, the above program attempts to call the private constructor of the abstract class indirectly via a public constructor.

Can abstract class have parameterized constructor in Java?

Yes, just like any normal class abstract class can have parameterized constructor in Java.

A parameterized constructor of the abstract class will initialize all or selected attributes of that abstract class. A subclass of abstract class can leverage the initialization code by calling parent class constructor using super keyword.

We’ve already depicted this in our first example.

Can we declare an abstract class without constructor?

Yes, we can declare an abstract class without a constructor just like a normal class. In this case, the responsibility of initialization of the member variables declared within the abstract class will come down to the subclasses.

Even if we do not provide a constructor, Java implicitly includes one no-org constructor within an abstract class which will be called by all the subclasses implicitly at the time of instantiating the subclass.

Abstract Class Constructor Rules

The rules of an abstract class constructor are similar to that of a regular class.

  • Name of the constructor must match with the class name
  • A constructor must not have a return type
  • Every class must have a constructor. If you do not provide Java will include one no-arg empty constructor for you.
  • You can have more than one constructor (constructor overloading) with unique set of arguments
  • super keyword must be the first statement in a constructor if you want to call super class constructor
  • this keyword must be the first statement in a constructor if you want to call another constructor from within the class
  • You can have all the access modifiers before the constructor name without return type

Leave a Comment