Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Constructor Overloading in Java – Explained with Practical Code Examples

What is a Constructor in Java?

A constructor is a special method that initializes a newly created object immediately after memory allocation. It sets the object’s fields to desired values or, if omitted, to their default values. Defining a constructor is optional; if none is provided, the compiler supplies a default one that assigns the standard default values:

This tutorial covers:

Rules for Creating a Java Constructor

  1. The constructor’s name must match the class name exactly.
  2. It cannot have a return type, not even void.

Example 1 – First Constructor

Step 1: Enter the following code into your Java editor.

class Demo {
    int value1;
    int value2;
    Demo() {
        value1 = 10;
        value2 = 20;
        System.out.println("Inside Constructor");
    }
    public void display() {
        System.out.println("Value1 === " + value1);
        System.out.println("Value2 === " + value2);
    }
    public static void main(String[] args) {
        Demo d1 = new Demo();
        d1.display();
    }
}

Step 2: Compile and run the program. Output:

Inside Constructor
Value1 === 10
Value2 === 20

Constructor Overloading in Java

Java allows a class to define multiple constructors with different parameter lists. The compiler distinguishes them by the number and types of parameters.

Valid constructors for a class named Account might look like:

Account(int a);
Account(int a, int b);
Account(String a, int b);

Example 2 – Overloaded Constructors

Step 1: Insert the following code.

class Demo {
    int value1;
    int value2;
    /* Demo() {
        value1 = 10;
        value2 = 20;
        System.out.println("Inside 1st Constructor");
    } */
    Demo(int a) {
        value1 = a;
        System.out.println("Inside 2nd Constructor");
    }
    Demo(int a, int b) {
        value1 = a;
        value2 = b;
        System.out.println("Inside 3rd Constructor");
    }
    public void display() {
        System.out.println("Value1 === " + value1);
        System.out.println("Value2 === " + value2);
    }
    public static void main(String[] args) {
        Demo d1 = new Demo();
        Demo d2 = new Demo(30);
        Demo d3 = new Demo(30, 40);
        d1.display();
        d2.display();
        d3.display();
    }
}

Step 2: Compile and run. You’ll encounter an error because the default constructor Demo() is missing after you added a parameterized constructor. To use the default constructor, explicitly declare it:

Demo() {
    // default initialization or custom logic
}

After uncommenting the commented block and re‑compiling, the program runs correctly.

Constructor Chaining

When a subclass is instantiated, its superclass constructor executes first—this is known as constructor chaining.

Example 3 – Chaining Between Parent and Child

Step 1: Copy the code below.

class Demo {
    int value1;
    int value2;
    Demo() {
        value1 = 1;
        value2 = 2;
        System.out.println("Inside 1st Parent Constructor");
    }
    Demo(int a) {
        value1 = a;
        System.out.println("Inside 2nd Parent Constructor");
    }
    public void display() {
        System.out.println("Value1 === " + value1);
        System.out.println("Value2 === " + value2);
    }
    public static void main(String[] args) {
        DemoChild d1 = new DemoChild();
        d1.display();
    }
}
class DemoChild extends Demo {
    int value3;
    int value4;
    DemoChild() {
        // super(5); // Uncomment to call the overloaded parent constructor
        value3 = 3;
        value4 = 4;
        System.out.println("Inside the Constructor of Child");
    }
    public void display() {
        System.out.println("Value1 === " + value1);
        System.out.println("Value2 === " + value2);
        System.out.println("Value3 === " + value3);
        System.out.println("Value4 === " + value4);
    }
}

Step 2: Run the program. Because the child constructor does not explicitly call super(), the default Demo() constructor runs first. Expected output:

Inside 1st Parent Constructor
Inside the Constructor of Child
Value1 === 1
Value2 === 2
Value3 === 3
Value4 === 4

Step 3: To invoke the overloaded parent constructor Demo(int a), place super(5); as the first line in the child constructor. After uncommenting the line and re‑running:

Inside 2nd Parent Constructor
Inside the Constructor of Child
Value1 === 5
Value2 === 0
Value3 === 3
Value4 === 4

Using super(...) is mandatory when you need a specific superclass constructor to run before the subclass logic.

Java

  1. Mastering Constructor Overloading in C#
  2. Java Hello World: Your First Program
  3. Master Java Constructors: Types, Usage, and Practical Examples
  4. Java Enum Constructors Explained with Practical Example
  5. Java OOP Concepts: Fundamentals, Examples, and Advantages
  6. Understanding Java Garbage Collection: How It Works & Why It Matters
  7. Constructor Overloading in Java – Explained with Practical Code Examples
  8. Java Multithreading Explained: Concepts, Lifecycle, and Practical Code Examples
  9. Java Program to Determine If a Number Is Prime
  10. Understanding Java Constructors: Initialization and Best Practices