Can an abstract class have a constructor?

An abstract class can have abstract methods and we can’t create its object. You can read Why we can’t create an object of the abstract class? But here a question strike in mind, Can abstract class have a constructor? If yes, then why and what is the use of constructor in abstract even we can’t create the object of an abstract class. Here we will discuss it in detail.

Yes, an abstract class can have a constructor in Java. The compiler automatically adds the default constructor in every class either it is an abstract class or concrete class. You can also provide a constructor to abstract class explicitly. The constructor is always used to initiate a class or use it to create an object of the class. But we can’t instantiate it with the new() operator or any other ways.

abstract class can have constructor

Why can an abstract class have constructors in Java?

I hope you have read the previous article in which we have discussed “Why can’t we create the object of an abstract class? Now we will learn why an abstract class has constructors.

To support the chaining of the constructor

By default, JVM is dealing with the constructor chaining because if you are creating an object by a new keyword then JVM invokes the constructor of that class and that constructor invokes the superclass constructor. If you want to know how constructor chaining works, then read the article.

To use an abstract class in Java, we use the concrete class that extends the abstract class and provides the implementation to the abstract methods. When we use the constructor of the child class, the constructor of the parent class invoked by child class constructor either implicitly or explicitly. This is one of the reasons abstract class can have constructors in Java. Suppose if JVM doesn’t provide the default constructor to an abstract class then JVM not able to support constructor chaining.

To initialize the fields of abstract class

As you know a constructor is used to initialize the fields of the class.  Suppose we want to set some common behavior for all the child classes and some behavior depends upon the subclass implementation. Then we can use the constructor of the abstractor class to initialize the fields.  

Let’s suppose we have an abstract class Car that has some common properties. These common properties will be common for each subclass that extends the Car class. We can initialize these fields in the Constructor of abstract class so, when you instantiate the constructor of child classes then the parent constructor will be called, and the fields will be initialized.

abstract class Car  
{
	String model;
	String brand;
	public abstract void addFeatures();
	Car()
	{
		this.model = "X5";
		this.brand = "BMW";
	}
}

class SportCar extends Car
{
	boolean nitro;
	@Override
	public void addFeatures() 
	{
		this.nitro = true;
	}
	public void showDetail()
	{
		System.out.println("Model :"+ model);
		System.out.println("Brand :"+ brand);
		System.out.println("Nitro :"+ nitro);
	}
}

public class MainClass
{
	public static void main(String arg[])
	{
		SportCar sportCar = new SportCar();
		sportCar.addFeatures();
		sportCar.showDetail();
	}
}

Output: Model :X5
Brand :BMW
Nitro :true

Leave a Comment