Types of Constructors With Java Code Examples

By | July 29, 2023

In Java, constructors play a crucial role in initializing objects during their creation. They handle memory allocation for the object and set initial values to its member variables. When we create an object using the new keyword, constructors are automatically invoked. Java offers three types of constructors:

Default Constructor

A default constructor is automatically generated by Java when no other constructors are explicitly defined in the class. It requires no parameters and contains an empty body. Its purpose is to set the object’s member variables with default values. If we don’t define any constructors in our class, the Java compiler will provide a default constructor.

Example of a default constructor

class Rectangle {
    private double length;
    private double width;

    // Default constructor
    public Rectangle() {
        length = 0.0;
        width = 0.0;
    }
}

Parameterized Constructor

  • A parameterized constructor is a constructor that accepts parameters, allowing you to initialize the object’s member variables with custom values at the time of object creation.
  • It enables you to provide specific values to the object’s state based on the arguments passed when the object is instantiated.
  • Here’s an example of a parameterized constructor for the Rectangle class:

class Rectangle {
    private double length;
    private double width;

    // Parameterized constructor
    public Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }
}


Copy Constructor

  • A copy constructor is a special constructor that takes an object of the same class as a parameter and creates a new object by copying the values of the member variables from the existing object.
  • It is used to create a deep copy of an object, which means a completely independent copy of the original object is created.
  • Here’s an example of a copy constructor for the Rectangle class:

class Rectangle {
    private double length;
    private double width;

    // Copy constructor
    public Rectangle(Rectangle other) {
        this.length = other.length;
        this.width = other.width;
    }
}

With the above constructors, you can create Rectangle objects in various ways. Examples of object creation and initialization using these constructors:


Rectangle rectangle1 = new Rectangle(); // Using default constructor
rectangle1.set(5.0, 3.0); // Setting values after object creation

Rectangle rectangle2 = new Rectangle(7.0, 4.0); // Using parameterized constructor

Rectangle rectangle3 = new Rectangle(rectangle2); // Using copy constructor

Each type of constructor allows you to initialize Rectangle objects with different sets of values, providing flexibility and convenience in object creation and initialization.

As per the provided example of the Rectangle class and the main method, the output will be:

Area of rectangle1: 15.0

Area of rectangle2: 28.0

Area of rectangle3: 28.0

Explanation:

  • rectangle1 is created using the default constructor, and its length and width are set to 5.0 and 3.0, respectively, using the set method. Therefore, the area of rectangle1 is 5.0 * 3.0 = 15.0.
  • rectangle2 is created using the parameterized constructor with length as 7.0 and width as 4.0. Therefore, the area of rectangle2 is 7.0 * 4.0 = 28.0.
  • Since `rectangle3` is created using the copy constructor and its values are copied from `rectangle2`, the area of `rectangle3` will be the same as `rectangle2`. Therefore, the area of `rectangle3` is also `28.0`.

After calculating the areas, the main method displays the areas of both rectangles using the System.out.println statement, resulting in the output mentioned above.

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *