Constructor Overloading in Java
In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object() can carry out a particular task is known as function Object() overloading.
The below program will describe the constructor overloading.
Example
Teacher.java
import java.io.*;
import java.util.*;
//this program is for constructor overloading
public class Teacher {
//declaring the instance variables of the class
int tid;
String tname;
Teacher() //constructor without parameters
{
System.out.println("default constructor/constructor with no parameters");
}
Teacher(int i, String n1) // constructor with having the parameters
{
tid = i; //assigning values
tname = n1;
}
//main section
public static void main(String[] args) {
//creation of object for the Teacher class
Teacher t = new Teacher();
System.out.println("\nThe values of the Default constructor: \n");
System.out.println("Teacher Id : "+t.tid + "\nTeacher's Name : "+t.tname); //printing the values of the default constructor
System.out.println("\nConstructor with having the parameters: \n"); //printing the values of the parameterised constructor
Teacher t1= new Teacher(100, "Ramu"); //creating object for passing the parameters
System.out.println("Teacher Id : "+t1.tid + "\nTeacher Name : "+t1.tname);
}
}
Output:

In the previous examples, there are two extra constructors-default and parameterised the Teacher class.
We must here comprehend the rationale for function Object() overloading. In certain cases, using several constructors is necessary to initialise different values of the class.
Moreover, we need to be aware that the Java compiler calls the default constructor when no constructors are used in the class. However, the default method is not called if we've used some methods in the class-default or parameterised. The function Object() is invalid in this situation, according to an exception that the java compiler raises.
Consider the example below in which the object is not created using the default constructor.
Students.java
public class Students {
String StudentsId;
Students(String StudentsId){
this.StudentsId = "Ravi " + StudentsId;
}
public static void main(String[] args) {
// calling the default constructor
Students clg = new Students(); //it doesn't create students constructor.
}
}
Output

By using this() method in the constructor overloading
This () method can also be used in the constructor. By using this keyword, one can access the elements of the parameterised constructor by passing the parameters in the this() method.
The below example will describe the use of this keyword.
Teacher.java
import java.io.*;
import java.util.*;
public class Teacher //creating the Teacher class
{
//declaring the instance variables of the class
int tid,tsalary;
String tname,contactNo,teachingCollegeName;
Teacher(String contactNo, String teachingCollegeName, int tsalary){
this.contactNo = contactNo;
this.teachingCollegeName = teachingCollegeName;
this.tsalary = tsalary;
}
Teacher(int tid, String tname) //parameterised constructor
{
this("9834563214", "IIT Hyderabad", 45000);
this.tid = tid;
this.tname = tname;
}
//main section
public static void main(String[] args) {
//creation of object for the Teacher class
Teacher t = new Teacher(101, "Sita");
System.out.println("Printing the details of the Teacher: \n");
System.out.println("Name: "+t.tname+"\nId: "+t.tid+"\nContact No: "+t.contactNo+"\nTeaching College Name: "+t.teachingCollegeName+"\nSalary: "+t.tsalary);
}
}
Output

There are some of the important steps that are to be followed in constructor overloading:
- When we want to call the constructor, it should be in the first line of the constructor.
- When no parameterised constructor is created, the compiler will first go to the default constructor.
- The constructor cannot be called in the recursive process.