Constructor Overloading vs Constructor Overriding in Java

Modified on Tue, 28 Mar, 2023 at 4:06 PM


Problem Manifestation: There is no specific error related to constructor overloading and overriding in Java, but users often get confused between these two concepts.


Description: Constructor overloading and constructor overriding are two important concepts in Java that are used to create objects of a class. Constructor overloading allows creating multiple constructors in the same class with different parameters, whereas constructor overriding is used to provide a new implementation of an existing constructor in the child class.


Understanding the difference:


  • Constructor Overloading:


To overload a constructor, you need to follow the steps given below:


  1. Create a class with the same name as the constructor you want to overload.

  2. Create multiple constructors with different parameters.

  3. When you create an object of the class, Java will automatically call the appropriate constructor based on the parameters passed.


Code Example:


public class Student {

int id;

String name;


// Default Constructor

Student() {

id = 100;

name = "John";

}


// Parameterized Constructor

Student(int i, String n) {

id = i;

name = n;

}


public static void main(String[] args) {

Student s1 = new Student();

Student s2 = new Student(101, "David");

System.out.println(s1.id + " " + s1.name);

System.out.println(s2.id + " " + s2.name);

}

}


  • Constructor Overriding:


To override a constructor, you need to follow the steps given below:


  1. Create a parent class with a constructor that needs to be overridden.

  2. Create a child class with the same name as the parent class.

  3. In the child class, create a constructor with the same name as the parent class.

  4. In the child class constructor, use the super keyword to call the constructor of the parent class.


Code Example:


class Parent {

Parent() {

System.out.println("Parent Constructor");

}

Parent(int x) {

System.out.println("The value of x is :" + x);

}

}


class Child extends Parent {

Child() {

super(5);

System.out.println("Child Constructor");

}


public static void main(String[] args) {

Child c = new Child();

}

}


Extra Information:


Constructor overloading and overriding are important concepts in Java that can be used to create objects of a class. Overloading is used to create multiple constructors with different parameters, whereas overriding is used to provide a new implementation of an existing constructor in the child class. It is essential to understand the difference between these two concepts to use them effectively in Java programs.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article