Constructor Overloading in Java with Example
We have already learned about overloading methods in our previous articles. This portion particularly explains overloading constructor in Java. Let’s start!!!
When does constructor overloading play a major role?
At times when we need to initialize an object in various ways, constructor overloading becomes helpful. This technique in Java lets a single class with many constructors that have a different list of arguments passed in it.
Identifying the Overloaded constructors in Java:
The Java compiler recognizes the overloaded constructors according to the parameter type, parameter lists and the number of input parameters. Therefore, the signature of a constructor is its name and the parameter types which are expected to be unique.
In case any two of the constructors contain an identical signature, an ambiguity arises. The compiler will not be able to differentiate; thus, it leads to an error.
Use of constructor overloading in Java:
This option lets us create an object of a specific class in various ways. Therefore, it helps a programmer to initialize the objects with different data types. Let us now see an example program to clear the air regarding this concept.
Sample program to implement java constructor overloading:
public class FirstCode{ int id; String course; FirstCode(){ System.out.println(“This is a default constructor”); } FirstCode(int i, String c) id = i; course = c; } public static void main(String args[]){ FirstCode fc = new FirstCode(); System.out.println(“The default constructor values are: \n”); System.out.println(“Course id: ”+fc.id + “\nCourse name: ”+fc.course); System.out.println(“The parameterized constructor values are: \n”); FirstCode fc2 = new FirstCode(5, “Java”); System.out.println(“Course id: ”+fc2.id + “\nCourse name: ”+fc2.course); } }
Output:
The default constructor values are:
Course id: 0
Course name: null
The parameterized constructor values are:
Course id: 5
Course name: Java
Program explanation:
In this program, the FirstCode class consists of two constructors, of which one is a default constructor, and the other one is a parameterized constructor. It is clear that the java compiler invokes a default constructor when no other constructor in the class is used. This will not occur if we invoke a constructor given in the class. In such cases, an error will be thrown indicating that the constructor is undefined.
Below is a sample program to demonstrate the occurrence of an error as the FirstCode object cannot be created with a default constructor as it does not have one.
public class FirstCode{ String course; FirstCode(String course){ this.course = “Java” + course; } public static void main(String args[]){ FirstCode fc = new FirstCode(); // here, a constructor FirstCode cannot be created. } }
Use of this() in Constructor overloading:
We have already seen the role of this() keyword in constructor chaining. Here, we use the ‘this()’ keyword inside a constructor to invoke another constructor that belongs to the same class.
Sample program to demonstrate this keyword in constructor overloading:
public class FirstCode{ int id, article; String course, topic, author; FirstCode(String course, String topic, int article){ this.course = course; this.topic = topic; this.article = article; } FirstCode(int id, String author){ this(“Java”, “Constructor”, 123); this.id = id; this.author = author; } public static void main(String args[]){ FirstCode fc = new FirstCode(91, “Benny Christopher”); System.our.println(“Course details\n”); System.out.println(“Article number: ”+fc.article + “\n Course Name:” +fc.name + “\nTopic” + fc.topic + “Id:” + fc.id + “Author Name:” + “author”); } }
Output:
Article number: 123
Course name: Java
Topic: Constructor
Id: 91
Author name: Benny Christopher
Summary
This was all about Constructor Overloading in Java. Hope you enjoyed the article.