GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Constructor
  • Overloading in Java
  • Introduction3 min read
  • Variables2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Constructor Overloading in Java - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Constructor Overloading in Java

Last Updated : 21 Jan, 2026

Java supports constructor overloading, which allows a class to have more than one constructor with different parameter lists. The appropriate constructor is selected at compile time based on the arguments passed during object creation. Constructor overloading enables objects to be initialized in multiple ways, improving flexibility and code clarity.

When do we need Constructor Overloading?

Constructor overloading is useful when:

  • Objects need to be initialized with different sets of data
  • Default values are required in some cases
  • Simplified object creation is desired for common use cases

Example:

The Thread class provides multiple constructors. For example:

Thread t = new Thread("MyThread");

If no arguments are required, the default constructor can be used. If a thread name is needed, a parameterized constructor is selected automatically.

Problem Without Constructor Overloading

Consider a class Box with only one constructor:

Java
class Box {
    double width, height, depth;

    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }
    double volume() {
        return width * height * depth;
    }
}

In this case, the following statement is invalid:

Box box = new Box(); // Compile-time error

The class does not support creating:

  • An empty box
  • A cube using a single value

Example of Constructor Overloading

By adding multiple constructors, different initialization options become available.

Java
class Box {
    double width, height, depth;

    Box(double w, double h, double d) {
        width = w;
        height = h;
        depth = d;
    }

    // No-argument constructor
    Box() {
        width = height = depth = 0;
    }

    // Constructor for cube
    Box(double len) {
        width = height = depth = len;
    }

    double volume() {
        return width * height * depth;
    }
}

// Driver class
public class Test {
    public static void main(String[] args) {

        Box mybox1 = new Box(10, 20, 15);
        Box mybox2 = new Box();
        Box mycube  = new Box(7);

        System.out.println("Volume of mybox1: " + mybox1.volume());
        System.out.println("Volume of mybox2: " + mybox2.volume());
        System.out.println("Volume of mycube: " + mycube.volume());
    }
}

Output
Volume of mybox1: 3000.0
Volume of mybox2: 0.0
Volume of mycube: 343.0

Using this() in Constructor Overloading

The this() keyword is used to call one constructor from another constructor in the same class. It helps avoid code duplication and ensures consistent initialization.

Java
class Box {
    double width, height, depth;
    int boxNo;

    Box(double w, double h, double d, int num) {
        width = w;
        height = h;
        depth = d;
        boxNo = num;
    }

    Box() {
        width = height = depth = 0;
    }

    Box(int num) {
        this(); // Calls the no-argument constructor
        boxNo = num;
    }

    public static void main(String[] args) {
        Box box1 = new Box(1);
        System.out.println(box1.width);
    }
}

Output
0.0

Rules for Using this() in Constructors

  • Constructor calls must be the first statement in the constructor.
  • Recursive constructor calls are not allowed.
  • A constructor can call only one other constructor.

Invalid Example:

Box(int num) {
boxNo = num;
this(); // Compile-time error
}

Note:

  • Constructors can be overloaded like methods, but they do not have return types.
  • If any constructor is defined, Java does not generate a default constructor.
  • Constructor overloading improves object initialization flexibility.
  • Overloading is resolved at compile time.

Constructor Overloading vs Method Overloading

Feature

Constructor Overloading

Method Overloading

Purpose

Initialize objects

Define multiple behaviors

Return type

Not allowed

Required

Name

Same as class

Any valid method name

Invocation

new keyword

Method call

Related Topics

  • Constructor
  • Overloading in Java
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.