Constructor Overloading in Java | Example Program
In Java, constructor overloading means to define multiple constructors but with different signatures. Constructor overloading is a technique of having more than one constructor in the same class with different parameter lists.
In other words, defining two or more constructors with the same name in a class but with different signatures is called constructor overloading. We arrange them in a such a way that each constructor performs a different task.
Java compiler differentiates these constructors based on the number of the parameter lists and their types. Therefore, the signature of each constructor must be different.
The signature of a constructor consists of its name and sequence of parameter types. If two constructors of a class have the same signature, it represents ambiguity. In this case, Java compiler will generate an error message because the compiler will be unable to differentiate which form to execute.
Hence, overloaded constructors must have different signatures. The compiler decides which constructor has to be called, depending on the number of arguments passing with objects.
The simple form of constructor overloading in Java is shown in the below figure.
Note: Overloading means more than one form. It refers to use of the same thing for a different purpose.
Simple Constructor Overloading Example
Let’s take a very simple example program to demonstrate constructor overloading in Java.
Example 1:
package constructorOverloadingPrograms;
public class Person
{
// Constructor overloading begins from here.
// Declare a non-parameterized constructor.
Person() {
System.out.println("Hello");
}
// Declare one parameterized constructor.
Person(String name) {
System.out.println(name);
}
// Main method.
public static void main(String[] args)
{
// Creating objects of class.
// JVM will call constructor depending on arguments passed.
Person p1 = new Person(); // calling non-parameterized constructor.
Person p2 = new Person("John"); // calling one parameterized constructor.
}
}
Output: Hello John
Let’s take another example program in which we will create three constructors with different signatures within the same class and call these overloaded constructors by passing different values to them.
Example 2:
public class Person
{
// Declaring a non-parameterized constructor.
Person() {
System.out.println("Introduction:");
}
// Declaring one parameterized constructor.
Person(String name){
System.out.println("Name: " +name);
}
// Declaring two parameterized constructor.
Person(String scname, int rollNo) {
System.out.println("School name: "+scname+ ", "+"Roll no:"+rollNo);
}
public static void main(String[] args)
{
// JVM will call constructor depending on arguments passed.
Person p1 = new Person(); // calling with zero argument.
Person p2 = new Person("John"); // calling with one argument.
Person p3 = new Person("DPS", 12); // calling with two arguments.
}
}
Output: Introduction: Name: John School name: DPS, Roll no:12
In this example program, we have created a class Person which has three overloaded constructors, first without any argument, second with one string argument, and third with string and int arguments.
When we have created an object of the class without passing any argument, JVM calls the non-parameterized constructor. Similarly, we have created an object of class by passing one argument of string type. In this case, JVM calls the one parameterized constructor.
At last, JVM calls two parameterized constructor when we have created an object of class by passing two argument values to it.
Use of Constructor Overloading in Java
Overloaded constructors are very common to use in Java programming based on needs because they provide many ways to create an object of a particular class. Constructor overloading allows initializing objects with different types of data.
For example, consider an object having three instance variables in a class. If we need to assign a specific value to the second instance variable and the default values to be assigned to the remaining variables.
In this case, it can be done by declaring multiple constructors based on different signatures in a class.
Constructor Overloading Example Programs for Best Practice
Let’s take an example program in which we will create three objects of class ‘School’ that will call three different constructors based on passing arguments.
Example 3:
package constructorOverloadingPrograms;
public class School
{
// Declare instance variables.
String scName;
int estYear;
// Constructor overloading begins from here.
// Create a non-parameterized constructor and initialize values.
// If we don't initialize values, default values null and 0 will print as output provided by default constructor.
School()
{
scName = "RSVM";
estYear = 1975;
}
// Create one parameter constructor and set the parameter name different from variable name.
// Because we are not using this reference.
School(String name)
{
scName = name;
}
// Create two parameters constructor and set the name of parameters different from name of variables.
School(String name, int year)
{
scName = name;
estYear = year;
}
// Create an instance method to print output.
void display()
{
System.out.println(scName+ " " +estYear);
}
// Main method declaration.
public static void main(String[] args)
{
// Create the first object with object reference variable sc.
School sc = new School(); // calling non-parameterized constructor.
// Create second object with object reference variable sc1.
School sc1 = new School("RSVM"); // calling one parameterized constructor.
// Create third object with object reference variable sc2.
School sc2 = new School("RSVM",1975); // calling two parameterized constructor.
// Now call methods using reference variables sc, sc1 and sc2 one by one to print output.
sc.display();
sc1.display();
sc2.display();
}
}
Output: RSVM 1975 RSVM 0 RSVM 1975
Explanation:
In the preceding program, there are three constructors of “School” class. The first form does not accept any argument. The scName and estYear instance variables are initialized with “RSVM” and 1975.
The second form accepts one string argument. This value is used to initialize the scName instance variable. Inside the second constructor, we have not initialized any value to the variable estYear. Therefore, the default constructor initializes a default value 0 to this variable.
The third form accepts two arguments string and int. These values are used to initialize scName and estYear instance variables. Thus, an overloaded constructor can initialize an object by using different types of data.
Let’s take another example program where an Employee class contains three constructors. Each of them will be used to initialize an object depending upon the values we have.
Example 4:
package constructorOverloadingPrograms;
public class Employee
{
Employee()
{
System.out.println("Employee Detail:");
}
Employee(String name)
{
System.out.println("Employee name: " +name);
}
Employee(String nCompany, int id)
{
System.out.println("Company name: " +nCompany);
System.out.println("Employee id: " +id);
}
}
public class Myclass
{
public static void main(String[] args)
{
Employee emp = new Employee();
Employee emp2 = new Employee("Deep");
Employee emp3 = new Employee("HCL", 12234);
}
}
Output: Employee Detail: Employee name: Deep Company name: HCL Employee id: 12234
The flow of execution of preceding program is shown in the below figure.
this Reference in Java
All instance methods or constructors accept an implicit argument called “this” which refers to the current object.
A current object is an object on which the method is called and ‘this’ reference can be used inside any method or constructor to refer to the current object.
Within the body of the method or constructor, ‘this’ reference can be used like any other object reference to access instance variables, instance methods, and constructors.
The main situation where the ‘this’ reference can be used, are as follows:
(a) When we set the name of the parameters different from the name of the instance variables inside the constructor, we can use ‘this’ reference to access the instance variables.
The general syntax to use this reference is as follows:
Syntax:
this.instance_variable_name = instance_variable_name;
(b) When we need to pass a reference to the current object as an argument to another method.
(c) When we need to call a constructor from another constructor, we can use this() syntax. The argument to this() must be matched with the target constructor and this() must be the first line inside the constructor.
By using this(), we can avoid duplicate code in multiple constructors, especially when the initialization routine is complex.
Overloading Example using this Reference
3. Let us take an example program where a class Student in which constructor Student() has an argument “name” which is similar to the name of the instance variable “name”.
To assign the value of formal argument (parameter) “name” to the instance variable “name”, we must use a dot(.) before the name of an instance variable.
Example 5:
package constructorOverloadingPograms;
public class Student
{
// Declaration of instance variables.
String name;
String schoolName;
int rollNo;
Student(String name, String sName)
{
this.name = name; // Here, 'this' reference is used.
schoolName = sName;
}
Student(String name, String sName, int rollNo)
{
this.name = name; // this reference.
schoolName = sName;
this.rollNo = rollNo;
}
void detail()
{
System.out.println(name+ " " +schoolName+ " " +rollNo);
}
public static void main(String[] args)
{
// Create objects of a class and pass arguments to their constructors.
Student st = new Student("Riddhi", "DPS");
Student st2 = new Student("Siddhi", "RSVM", 05);
st.detail();
st2.detail();
}
}
Output: Riddhi DPS Siddhi RSVM 05
Let’s consider an example program where we will use access modifiers, encapsulation, constructor overloading, and this reference. Look at the source code to understand better.
Example 6:
package constructorOverloadingPrograms;
public class Number
{
// Declare the variable as private.
private int num = 300; // Encapsulated code.
// Declaration of Overloading constructor.
public Number()
{
// Here, you can also initialize the value of instance variable.
}
Number(int num)
{
this.num = num;
}
// Create getter method for private variable.
public int getNum()
{
return num;
}
// Create the public setter method for variable.
public void setNum(int num)
{
this.num = num; // this reference used in method.
}
}
Create another class NumberTest and write the main method.
public class NumberTest {
public static void main(String[] args)
{
Number n1 = new Number();
Number n2 = new Number();
Number n3 = new Number(500);
Number n4 = new Number(600);
n2.setNum(400); // assigning new value.
System.out.println(" First Number: " +n1.getNum());
System.out.println(" Second Number: " +n2.getNum());
System.out.println(" Third Number: " +n3.getNum());
System.out.println(" Fourth Number: " +n4.getNum());
}
}
Output: First Number: 300 Second Number: 400 Third Number: 500 Fourth Number: 600
If you have any problem understanding the Encapsulation in Java concepts, you can learn Encapsulation in a very easy way and step by step. Practice all the programs of Encapsulation to clear the concepts.
Let’s consider one more example program based on the combination of encapsulation, constructor overloading, and this keyword. Look at the program code.
Example 7:
package constructorOverloadingPrograms;
public class Fraction
{
private int num; // Numerator is encapsulated. So, we cannot access it directly from outside the class.
private int deno; // Denominator is encapsulated.
Fraction(int n, int d)
{
// Declaration and initialization of values of instance variables.
num = 100;
deno = 200;
}
Fraction()
{
this(1,1); // Calling current class parameterized constructor.
}
// Create getter methods to read the values because we cannot read values directly from outside the class.
public int getNum()
{
return num;
}
public int getDeno()
{
return deno;
}
}
public class FractionTest
{
// Main method.
public static void main(String[] args)
{
// Non-parameterized constructor will be called after object creation.
Fraction obj = new Fraction();
// Call getter method using object reference variable obj to read values of variables.
System.out.println(+obj.getNum());
System.out.println(+obj.getDeno());
}
}
Output: 100 200
Advantage of Constructor Overloading
The advantages of using constructor overloading in Java programming are as follows:
- Constructor overloading helps to achieve static polymorphism in Java.
- The main advantage of constructor overloading is to allow an instance of a class to be initialized in various ways.
- It allows to define of the multiple constructors of a class with different signatures.
- It helps to perform different tasks for different purposes.
Key Points of Constructor Overloading:
- Overloaded constructors means more than one constructor of a class with different signatures.
- To compile each constructor must have different parameter lists.
- A parameter list consists of order and types of arguments.
- We cannot have two constructors in a class with the same parameter lists.