What is Constructor Overloading in Java?

Constructor overloading is an important concept in Java that allows a class to have multiple constructors with different parameter lists. It helps initialize objects in different ways depending on the requirement.

This concept is commonly asked in Java interviews and widely used in real-world applications.


What is a Constructor?

A constructor is a special method in Java that:

  • Has the same name as the class

  • Does not have a return type

  • Is automatically called when an object is created

Its main purpose is to initialize object values.


What is Constructor Overloading?

Constructor Overloading occurs when a class contains more than one constructor with the same name but different parameters.

👉 The constructor executed depends on the arguments passed during object creation.


Syntax

class ClassName { ClassName() { // default constructor } ClassName(int a) { // parameterized constructor } ClassName(int a, String b) { // another constructor } }

Example Program

class Student { String name; int age; // Default constructor Student() { name = "Unknown"; age = 0; } // Parameterized constructor Student(String n, int a) { name = n; age = a; } void display() { System.out.println(name + " " + age); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Rahul", 22); s1.display(); s2.display(); } }

Output

Unknown 0 Rahul 22

Explanation

  • The class has two constructors.

  • One initializes default values.

  • Another initializes custom values.

  • Java decides which constructor to call based on parameters.

This is an example of compile-time polymorphism.


Advantages of Constructor Overloading

✔ Provides multiple ways to create objects
✔ Improves code flexibility
✔ Enhances readability
✔ Supports object initialization easily
✔ Reduces the need for setter methods


Important Rules

  • Constructor name must match the class name.

  • Constructors must differ in parameter list.

  • Changing only return type is not allowed.

  • Constructors can call each other using this().


Interview Tip

A simple definition:

Constructor overloading is the process of defining multiple constructors in a class with different parameter lists to initialize objects in different ways.


Conclusion

Constructor overloading makes object creation flexible and efficient. It is widely used in real-time Java applications where objects need different initialization options.


🚀 No 1 Java Real Time Projects Online Training in Hyderabad — ashok it

Want to learn Java concepts with real-time project experience and industry-focused training?

Join our job-oriented Java Full Stack training program guided by real time experts.

✔ Core Java & Advanced Java
✔ Spring Boot & REST API Development
✔ Microservices Architecture
✔ Real-Time Industry Projects
✔ Interview Preparation & Placement Assistance

Comments

Popular posts from this blog

What is Reflection API in Java?

Spring Boot Microservices Development

Recursion & Dynamic Programming