Constructor in Abstract Class in Java

An Abstract class in Java is a class that cannot be instantiated directly. The goal of this is to act as a base class from which the other classes might inherited and extended. One of the important features that an abstract class has an ability to define the constructors, which are known as special methods and that are invoked when an object of a class is created.

Rules to be followed while defining constructors in an abstract class:

  1. Abstract classes can have constructors, but they cannot be instantiated directly. The constructors are used when a concrete subclass is created.
  2. There may be one or greater abstract methods in an abstract class, which signifies that those methods are not implemented by means of the class. To be instantiated, a subclass that extends an abstract class with abstract methods should implement the methods. It means that each abstract method declared within an abstract class need to have an implementation if a subclass needs to be a concrete class and be able to be instantiated. In other words, the functionality that the abstract class left open must be filled in by the subclass.
  3. When a subclass extends an abstract class with constructors, the subclass needs to call one of the constructors inside the superclass with the help of super key-word. Because the superclass constructor initializes the state of the object and units up any vital resources. If the subclass does now not call one of the constructors in the superclass, the object will not be well initialized and will now not function efficiently/correctly.
  4. It is feasible to define more than one constructor in an abstract class, similar to some other class. However, each constructor has to be defined with a different parameter list. It lets in subclasses to pick out which constructor to call based on their specific needs.

Types of Constructors implemented using Abstract Class:

There are three types of constructors are there they are:

  1. Default Constructor
  2. Parameterized Constructor
  3. Copy Constructor

1. Default Constructor: The constructor is automatically created via Java if no other constructor is defined in the class. It has no parameters and does not perform any moves aside from initializing default values for class fields.

ALGORITHM:

Step 1: Define an abstract class named "Shape".

Step 2: Declare two integer variables "x" and "y" as protected.

Step 3: Create a Shape class default constructor and set "x" and "y" to 0.

Step 4: Now create a method "getArea()" it is a abstract method which is going to return a double value

Step 5: Then create two non-abstract methods "printPosition()" and "setPosition(int x, int y)" which belongs to Shape class.

Step 6: The setPosition method sets the values of x and y.

Step 7: The printPosition method prints the values of x and y.

Step 8: Define a Circle class that extends the Shape class.

Step 9: Declare a double variable named "radius" as protected in the Circle class.

Step 10: Define a constructor for the Circle class that accepts a double value for the radius.

Step 11: Implement the getArea method for the Circle class which calculates the area of the circle.

Step 12: Define a Square class that extends the Shape class.

Step 13: Declare a double variable named "side" as protected in the Square class.

Step 14: Define a constructor for the Square class that accepts a double value for the side.

Step 15: Implement the getArea method for the Square class which calculates the area of the square.

Step 16: Define a Main class.

Step 17: Define the main function in the Main class.

Step 18: Create a Circle object and a Square object.

Step 19: Call the setPosition method for both the Circle and Square objects.

Step 20: Call the getArea method for both the Circle and Square objects and print the results.

Step 21: Call the printPosition method for both the Circle and Square objects and print the results.

Implementation:

Here is the implementation of above steps

FileName: DefaultMain.java

Output:

Area of a circle is: 78.53981633974483
The Position:(2, 3)
Area of a square is: 16.0
The Position:(5, 7)

2. Parameterized Constructor: When creating an object, this kind of constructor lets you pass arguments to it. When you wish to initialise the object with values, it is helpful. The parameterized constructor is defined with one or extra parameters, and whilst an object is created, the values passed to the constructor are used to initialize the corresponding fields of the item.

ALGORITHM:

Step 1: Define an abstract class Shape.

Step 2: Add two protected instance variables of type int named x and y.

Step 3: Create a parameterized constructor that initialises the instance variables x and y and accepts two parameters of type int, x and y.

Step 4: Define an abstract class Shape.

Step 5: Add two protected instance variables of type int named x and y.

Step 6: Create a parameterized constructor that initialises the instance variables x and y and accepts two parameters of type int, x and y.

Step 7: Define a class Circle that extends Shape.

Step 8: Add a protected instance variable of type double named radius.

Step 9: Define a parameterized constructor that takes three parameters of type int x, y, and double radius and initializes the x, y and radius instance variables using super() keyword.

Step 10: Implement the abstract method getArea() by calculating the area of Circle.

Step 11: Define a class Square that extends Shape.

Step 12: Add a protected instance variable of type double named side.

Step 13: Define a parameterized constructor that takes three parameters of type int x, y, and double side and initializes the x, y and side instance variables using super() keyword.

Step 14: Implement the abstract method getArea() by calculating the area of Square.

Step 15: Define a class Main.

Step 16: Define a static method named main() which is the entry point of the program.

Step 17: Create a Circle object using parameterized constructor.

Step 18: Print the area and position of the Circle object using getArea() and printPosition() methods respectively.

Step 19: Create a Square object using parameterized constructor.

Step 20: Print the area and position of the Square object using getArea() and printPosition() methods respectively.

Step 21: End of the program.

Implementation:

The implementation of the above steps mentioned below

FileName: ParameterizedMain.java

Output:

Area of circle is: 78.53981633974483
The position: (2, 3)
Area of square is:16.0
The position: (5, 7)

3. Copy constructor: copy constructor is used to create a new object with the equal values as an existing object (i.e., the item is created earlier than). It is useful while we need to create a new object that may be a replica of an object that is already present/existed. Copy constructor is defined with only one argument or one parameter that is an item of identical class. Then the constructor creates new object with same values as a parameter object.

ALGORITHM:

Step 1: Declare an abstract class with instance variables and default constructor.

Step 2: Define a copy constructor with a parameter of the identical class type.

Step 3: In the copy constructor, call the superclass copy constructor using the super keyword to copy the instance variables from the parameter object to the new object.

Step 4: Assign the values of any extra instance variables within the subclass to the new item.

Step 5: Implement the abstract method to calculate the area.

Step 6: Define any other methods as necessary.

Step 7: In the main function, create an object of the class.

Step 8: Set the position and any other instance variables as necessary.

Step 9: Create a new object the use of the copy constructor and passing the original item as a parameter.

Step 10: Print the area and position of both the original and copied objects.

Implementation:

The implementation of above steps is given below

FileName: CopyMain.java

Output:

Original Area of circle: 78.53981633974483
Position: (2, 3)
Copied Area of circle: 78.53981633974483
Position: (2, 3)
Original Area of square: 16.0
Position: (5, 7)
Copied Area of square: 16.0
Position: (5, 7)