DEV Community

Athithya Sivasankarar
Athithya Sivasankarar

Posted on

Understanding Constructors and Constructor Overloading in Java

What is a Constructor?

A constructor is a special method that is automatically called whenever an object of a class is created.

Unlike normal methods, a constructor:

  • Has the same name as the class.
  • Does not have a return type (not even void).
  • Is executed automatically when an object is created using the new keyword.

Syntax

class ClassName {

    ClassName() {
        // Constructor body
    }

}
Enter fullscreen mode Exit fullscreen mode

Why Do We Need Constructors?

Constructors help us:

  • Initialize object variables.
  • Reduce repetitive code.
  • Ensure objects start with valid values.
  • Improve code readability.

Example 1: Default Constructor

class Student {

    Student() {
        System.out.println("Student object created successfully.");
    }

    public static void main(String[] args) {

        Student s1 = new Student();

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Student object created successfully.
Enter fullscreen mode Exit fullscreen mode

Explanation

When the statement

Student s1 = new Student();
Enter fullscreen mode Exit fullscreen mode

is executed,

Java automatically calls the constructor

Student()
Enter fullscreen mode Exit fullscreen mode

which prints the message.

Example 2: Constructor Initializing Variables

class Product {

    String name;
    int price;

    Product() {
        name = "Rice";
        price = 1500;
    }

    void display() {
        System.out.println("Product Name : " + name);
        System.out.println("Price : " + price);
    }

    public static void main(String args[]) {

        Product p1 = new Product();

        p1.display();

    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Product Name : Rice
Price : 1500
Enter fullscreen mode Exit fullscreen mode

Explanation

The constructor assigns default values to the object variables. Whenever a new object is created, these values are automatically initialized.

Types of Constructors

Java mainly has two types of constructors.

1. Default (No-Argument) Constructor

A constructor that does not take any parameters.

Example:

Student() {

}
Enter fullscreen mode Exit fullscreen mode

2. Parameterized Constructor

A constructor that accepts parameters.

Example:

Student(String name, int age) {

}
Enter fullscreen mode Exit fullscreen mode

Parameterized constructors allow us to initialize objects with different values.

What is Constructor Overloading?

Constructor Overloading means having multiple constructors in the same class with different parameter lists.

Each constructor performs a different type of object initialization.

Java determines which constructor to call based on the arguments passed while creating the object.

Example of Constructor Overloading

class Employee {

    String name;
    int age;

    Employee() {
        name = "Unknown";
        age = 0;
    }

    Employee(String n) {
        name = n;
        age = 0;
    }

    Employee(String n, int a) {
        name = n;
        age = a;
    }

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String args[]) {

        Employee e1 = new Employee();

        Employee e2 = new Employee("Athithya");

        Employee e3 = new Employee("Athithya", 20);

        e1.display();

        e2.display();

        e3.display();

    }

}
Enter fullscreen mode Exit fullscreen mode

Output

Unknown 0
Athithya 0
Athithya 20
Enter fullscreen mode Exit fullscreen mode

How Constructor Overloading Works

When these statements execute,

Employee e1 = new Employee();
Enter fullscreen mode Exit fullscreen mode

Java calls

Employee()
Enter fullscreen mode Exit fullscreen mode

When

Employee e2 = new Employee("Athithya");
Enter fullscreen mode Exit fullscreen mode

Java calls

Employee(String n)
Enter fullscreen mode Exit fullscreen mode

When

Employee e3 = new Employee("Athithya", 20);
Enter fullscreen mode Exit fullscreen mode

Java calls

Employee(String n, int a)
Enter fullscreen mode Exit fullscreen mode

Java chooses the constructor based on the number and type of arguments provided.

Advantages of Constructor Overloading

  • Provides multiple ways to initialize objects.
  • Makes programs more flexible.
  • Improves code readability.
  • Reduces the need for multiple initialization methods.
  • Supports object-oriented programming principles.

Difference Between Constructors and Methods

Constructor Method
Same name as the class Can have any valid name
No return type Has a return type or void
Called automatically Called explicitly
Used to initialize objects Used to perform operations

Top comments (0)