Abstract Class Constructor in Java
What is a constructor in java?
In Java, a constructor is help us to create the object and it also helps to initialise the objects that belongs to a class. If we want to create a class object and a constructor to execute the initialised object then we use “new” keyword.
As we all know, a constructor does not have a return type and it is not even void and shares the same name as the class name. It may have zero or more parameters, which the constructor receives as values when creating an object.
The main purpose of a constructor is to initialise the data members (fields) of an object with predetermined or default values. The constructor of a class is automatically invoked when an object of that class is created and has the ability to do any initialization activities required for the object to be in a useable state.
For example:
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } }
What is an abstract class constructor?
An abstract class constructor in Java is a unique method used to create and initialise instances of a class that can only be sub-classed and cannot be instantiated directly. The initialization of data members shared by all subclasses and other common initialization activities are done in an abstract class constructor.
Because it is unfinished and has one or more abstract methods that are not implemented, an abstract class cannot be instantiated directly. As a result, whenever a concrete subclass is instantiated, an abstract class constructor is called.
For example:
Let’s take an example of an abstract class constructor in java:
public abstract class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } public abstract double area(); }
Explanation of above example
As you see we have an abstract class called Shape and it has a constructor with represent the parameter color of the shape in this example. We used constructor to initialize the color data member of the object with the value passed in as a parameter.
We have an abstract method called area () in the shape class that is not implemented in the Shape class. If we want to implement the area () method then we must need to implement the area () method in the concrete subclass that extends the shape class.
The Shape class's constructor is invoked when a concrete subclass of Shape is created in order to initialise the object's data members that are shared by all subclasses, including the colour data member.
In conclusion, a Java abstract class constructor is a unique method used to create and initialise objects of a class that cannot be directly instantiated but can only be subclassed. The initialization of data members shared by all subclasses and other common initialization chores are done in the abstract class constructor.
Uses of abstract class constructor
There are some main uses of an abstract class constructor in java as follows:
Data member’s initialization: we initialised the data member of an abstract class with the use of constructor of the abstract class. All subclasses that derive from the abstract class share these data elements. Data members can be appropriately initialised in all subclasses if they are initialised in the abstract class constructor.
It enforce the contract between an abstract class and its subclasses: In java, an abstract class constructor requires the subclass to submit values for specific data members during the time of object formation in order to enforce the contract between an abstract class and its subclasses. This makes sure that the subclasses offer the data that the abstract class needs.
Common initialization tasks: All subclasses that derive from the abstract class must complete the common initialization chores, which can be accomplished by the abstract class constructor. It also encourages better code organisation by reducing code duplication.
Keeps abstract class from being instantiated: we use an abstract class constructor so we can keep an abstract class from being instantiated. It help us to ensure that only subclasses can be instantiated by making the constructor protected or private
Ensure that a subclass constructor calls the superclass constructor: in subclass of an abstract class we must call the constructor of the abstract class when it is formed in order to initialise the data members that are declared in the abstract class. By doing this, it is made sure that the superclass's data members are correctly initialised before the subclass's constructor runs.