Constructor Overloading in Java Programming Language

Rumman Ansari   Software Engineer   2023-03-09   9175 Share
☰ Table of Contents

Table of Content:


Constructor overloading

The constructor has exactly the same name as its defining class. Like regular methods, constructors can be overloaded (i.e., multiple constructors can have the same name but different signatures), making it easy to construct objects with different initial data values.

When do we need Constructor Overloading?

Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading. For example, Thread class has 6 types of constructors. If we do not want to specify anything about a thread then we can simply use default constructor of Thread class,

Thread t= new Thread (); 
however if we need to specify thread name, then we may call the parameterized constructor of Thread class with a String args like this:

Thread t= new Thread ("Thread1"); 

Example of Constructor Overloading

Save file: StudentClass.java

class StudentClass{
	String name;
    int rollNo;
    int year;
    StudentClass(String n,int i){
    name = n;
    rollNo = i;
    }
    StudentClass(String n,int i,int y){
    name = n;
    rollNo = i;
    year=y;
    }
    void display(){
		System.out.println("Roll NO: "+rollNo+" Name: "+name+" Year: "+year);
		}

    public static void main(String args[]){
    StudentClass s1 = new StudentClass("Dhinchak Pooja",25);
    StudentClass s2 = new StudentClass("Hero Alom",22,2);
    s1.display();
    s2.display();
   }
}

Output:

Roll NO: 25 Name: Dhinchak Pooja Year: 0 Roll NO: 22 Name: Hero Alom Year: 2 Press any key to continue . . .

When will a class have a default constructor?

A class has a default constructor only if the class does not define any constructor.

Copy Constructor

There is no copy constructor in java. But, we can copy the values of one object to another like copy constructor in C++. There are many ways to copy the values of one object into another in java. They are:

  1. By constructor
  2. By assigning the values of one object into another
  3. By clone() method of Object class

In this example, we are going to copy the values of one object into another using java constructor.

class Rectangle{
    int height;
    int width;
    Rectangle(int h,int w){
    height = h;
    width = w;
    }

    Rectangle(Rectangle s){
    height = s.height;
    width =s.width;
    }
    void display(){
		System.out.println(height+" "+width);
		}

    public static void main(String args[]){
    Rectangle s1 = new Rectangle(11,12);
    Rectangle s2 = new Rectangle(s1);
    s1.display();
    s2.display();
   }
}

Output

11 12
11 12
Press any key to continue . . .

Copy values without constructor

We can copy the values of one object into another by assigning the objects values to another object. In this case, there is no need to create the constructor.

class Rectangle{
    int height;
    int width;
    Rectangle(int h,int w){
    height = h;
    width = w;
    }

    Rectangle(Rectangle s){
    height = s.height;
    width =s.width;
    }

    Rectangle(){
    }

    void display(){
		System.out.println(height+" "+width);
		}

    public static void main(String args[]){
    Rectangle s1 = new Rectangle(13,14);
    Rectangle s2 = new Rectangle();
    s2.height = s1.height ;
    s2.width = s1.width ;
    s1.display();
    s2.display();
   }
}

Output:

13 14
13 14
Press any key to continue . . .