Skip to content

Overloaded Constructors

Overloaded constructors in Java allow a class to have more than one constructor with different parameter lists. This enables the creation of objects with varying initialization values or behaviors depending on the constructor called.


Key Features of Constructor Overloading

  1. Multiple Constructors: A class can have multiple constructors, each with a different number or type of parameters.
  2. Compile-Time Polymorphism: The constructor to be called is determined at compile-time based on the arguments provided.
  3. Code Reusability: Reduces code duplication by allowing constructors to initialize objects in different ways.

Rules for Overloading Constructors

  1. The constructors must differ in:
    1. The number of parameters, or
    1. The type of parameters, or
    1. The order of parameters.
  2. Overloaded constructors can have different access modifiers.

Example: Overloaded Constructors

Code Example

class Student {

    String name;

    int age;

    String course;

    // Constructor 1: No parameters

    Student() {

        name = “Unknown”;

        age = 0;

        course = “Not Enrolled”;

    }

    // Constructor 2: One parameter

    Student(String n) {

        name = n;

        age = 0;

        course = “Not Enrolled”;

    }

    // Constructor 3: Two parameters

    Student(String n, int a) {

        name = n;

        age = a;

        course = “Not Enrolled”;

    }

    // Constructor 4: Three parameters

    Student(String n, int a, String c) {

        name = n;

        age = a;

        course = c;

    }

    void display() {

        System.out.println(“Name: ” + name + “, Age: ” + age + “, Course: ” + course);

    }

}

public class Main {

    public static void main(String[] args) {

        // Using different constructors

        Student s1 = new Student();

        Student s2 = new Student(“Alice”);

        Student s3 = new Student(“Bob”, 20);

        Student s4 = new Student(“Charlie”, 22, “Computer Science”);

        // Display student details

        s1.display();

        s2.display();

        s3.display();

        s4.display();

    }

}

Output:

Name: Unknown, Age: 0, Course: Not Enrolled

Name: Alice, Age: 0, Course: Not Enrolled

Name: Bob, Age: 20, Course: Not Enrolled

Name: Charlie, Age: 22, Course: Computer Science


Constructor Chaining in Overloaded Constructors

Constructor chaining occurs when one constructor calls another constructor in the same class to reuse code. This is done using the this() keyword.

Code Example: Constructor Chaining

class Employee {

    String name;

    int age;

    double salary;

    // Constructor 1: No parameters

    Employee() {

        this(“Unknown”, 0, 0.0); // Calls Constructor 3

    }

    // Constructor 2: One parameter

    Employee(String n) {

        this(n, 0, 0.0); // Calls Constructor 3

    }

    // Constructor 3: All parameters

    Employee(String n, int a, double s) {

        name = n;

        age = a;

        salary = s;

    }

    void display() {

        System.out.println(“Name: ” + name + “, Age: ” + age + “, Salary: ” + salary);

    }

}

public class Main {

    public static void main(String[] args) {

        Employee e1 = new Employee();

        Employee e2 = new Employee(“Alice”);

        Employee e3 = new Employee(“Bob”, 25, 50000);

        e1.display();

        e2.display();

        e3.display();

    }

}

Output:

Name: Unknown, Age: 0, Salary: 0.0

Name: Alice, Age: 0, Salary: 0.0

Name: Bob, Age: 25, Salary: 50000.0


Advantages of Constructor Overloading

  1. Flexibility: Enables object creation with different initial states based on provided parameters.
  2. Readability: Simplifies code by logically grouping initialization logic within constructors.
  3. Reusability: Reduces code duplication through constructor chaining.

Common Mistakes in Constructor Overloading

  1. Ambiguity in Arguments: If the compiler cannot distinguish between overloaded constructors due to similar parameter types, an error occurs.

class Example {

    Example(int x, float y) {}

    Example(float x, int y) {}

}

// This leads to ambiguity:

Example obj = new Example(5, 5); // Both match int → float and float → int

  • Not Using this(): Without chaining, initialization logic may need to be repeated across constructors.

Conclusion

Overloaded constructors enhance the flexibility and efficiency of object initialization in Java. With constructor chaining, they provide a powerful tool for maintaining clean and reusable code. By understanding and using overloaded constructors effectively, developers can handle complex initialization scenarios effortlessly.