Skip to main content

Command Palette

Search for a command to run...

Constructor Overloading in Java – A Complete Guide

Published
3 min read

Last Updated: 2026


Introduction

Java is an Object-Oriented Programming (OOP) language, and the first step in OOP is object creation.
Whenever an object is created in Java, a constructor is automatically called.

In real-world programs, we do not always want to create objects in the same way.
Sometimes we want to create an object without data, sometimes with partial data, and sometimes with full data.

👉 To handle these different situations, Java provides a feature called Constructor Overloading.


What is Constructor Overloading?

Constructor Overloading means:

Defining more than one constructor in the same class with different parameter lists.

Important Points

  • Constructor name is always the same as the class name

  • Difference is only in:

    • number of parameters

    • type of parameters

    • order of parameters


Why Do We Need Constructor Overloading?

Constructor overloading allows:

  • Multiple ways to create an object

  • Flexibility while initializing objects

  • Cleaner and more readable code

Example Use Case

  • Create an object with default values

  • Create an object with only name

  • Create an object with name and id

All this is possible using constructor overloading.


How Java Chooses the Correct Constructor?

Java decides at compile time based on:

  1. Number of arguments passed

  2. Type of arguments

  3. Order of arguments

This is called compile-time polymorphism.


Types of Constructor Overloading

Constructor overloading can happen in three ways:

  1. By changing the number of parameters

  2. By changing the type of parameters

  3. By changing the order of parameters

Let’s understand each one with examples.


1️⃣ Constructor Overloading by Number of Parameters

Example

class Demo {

    Demo() {
        System.out.println("No argument constructor");
    }

    Demo(int a) {
        System.out.println("One argument constructor");
    }

    Demo(int a, int b) {
        System.out.println("Two argument constructor");
    }

    public static void main(String[] args) {
        new Demo();
        new Demo(10);
        new Demo(10, 20);
    }
}

Output

No argument constructor
One argument constructor
Two argument constructor

Explanation

  • Demo() → 0 parameters

  • Demo(10) → 1 parameter

  • Demo(10, 20) → 2 parameters

✔ Different number of parameters → constructor overloading


2️⃣ Constructor Overloading by Type of Parameters

Example

class Demo {

    Demo(String name) {
        System.out.println("String constructor: " + name);
    }

    Demo(long id) {
        System.out.println("Long constructor: " + id);
    }

    public static void main(String[] args) {
        new Demo("Amit");
        new Demo(101);
    }
}

Output

String constructor: Amit
Long constructor: 101

Explanation

  • Same number of parameters (1)

  • Different types (String and long)

✔ Different parameter type → constructor overloading


3️⃣ Constructor Overloading by Order of Parameters

Example (Tricky but Important)

class Demo {

    Demo(int a, String s) {
        System.out.println("int, String constructor");
    }

    Demo(String s, int a) {
        System.out.println("String, int constructor");
    }

    public static void main(String[] args) {
        new Demo(10, "Hello");
        new Demo("Hello", 10);
    }
}

Output

int, String constructor
String, int constructor

Explanation

  • Same parameter types

  • Same number of parameters

  • Different order

✔ Order difference also causes constructor overloading


Copy Constructor as an Example of Overloading

Although Java does not provide a built-in copy constructor, we can create one manually.

Example

class Student {

    String name;
    int id;

    Student(String n, int i) {
        name = n;
        id = i;
    }

    // Copy Constructor
    Student(Student s) {
        name = s.name;
        id = s.id;
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul", 50);
        Student s2 = new Student(s1);

        System.out.println(s2.name);
        System.out.println(s2.id);
    }
}

Explanation

  • One constructor takes (String, int)

  • Another takes (Student)

  • Same class, different parameter list

✔ This is constructor overloading


❌ What is NOT Constructor Overloading?

Demo(int a)
Demo(int b)

❌ This is NOT constructor overloading
Java does not care about variable names.
It only checks:

  • number

  • type

  • order