What is Constructor Overloading in Java?

In this example you will understand What is Constructor Overloading in Java? You will also find the example of Constructor Overloading in Java.

What is Constructor Overloading in Java?

In this example you will understand What is Constructor Overloading in Java? You will also find the example of Constructor Overloading in Java.

Constructor Overloading in Java

What is Constructor Overloading in Java with examples?

     

In this tutorial you will learn about the construction overloading in Java and understand this concept with the example. In Java you can have a class with multiple constructor with different parameter. Java allows you to create a class with multiple constructor.

The multiple constructor allows you to instantiate your class by passing different values for the constructor.

We have developed an example class here which take different parameters in constructor definition. We have Rectangle class with following constructor definitions:

  • Rectangle(int x, int y)
  • Rectangle(int x)
  • Rectangle(float x)
  • Rectangle(float x, float y)

So, this way you can define as many constructor as you want based on your requirement.

Constructors are used to assign initial values to instance variables of the class. A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM). Constructor is always called by new operator. Constructor are declared just like as we declare methods, except that the constructor don't have any return type. Constructor can be overloaded provided they should have different arguments because JVM differentiates constructors on the basis of arguments passed in the constructor.

Whenever we assign the name of the method same as  class name. Remember this method should not have any return type. This is called as constructor overloading. 

We have made one program on a constructor overloading, after going through it the concept of constructor overloading will get more clear. In the example below we have made three overloaded constructors each having different arguments  types so that the JVM can differentiates between the various constructors.

The code of the program is given below:

public class ConstructorOverloading{
  public static void main(String args[]){
  Rectangle rectangle1=new Rectangle(2,4);
  int areaInFirstConstructor=rectangle1.first();
  System.out.println(" The area of a rectangle in 
  first constructor is :  " 
+ areaInFirstConstructor);
  Rectangle rectangle2=new Rectangle(5);
  int areaInSecondConstructor=rectangle2.second();
  System.out.println(" The area of a rectangle in 
   first constructor is :  " 
+ areaInSecondConstructor);
  Rectangle rectangle3=new Rectangle(2.0f);
  float areaInThirdConstructor=rectangle3.third();
  System.out.println(" The area of a rectangle in first 
  constructor is :  " 
+ areaInThirdConstructor);
  Rectangle rectangle4=new Rectangle(3.0f,2.0f);
  float areaInFourthConstructor=rectangle4.fourth();
  System.out.println(" The area of a rectangle in first 
  constructor is :  " 
+ areaInFourthConstructor);
  }
}

class Rectangle{
  int l, b;
  float p, q;
  public Rectangle(int x, int y){
  l = x;
  b = y;
  }
  public int first(){ 
  return(l * b);
  }
  public Rectangle(int x){
  l = x;
  b = x;
  }
  public int second(){
  return(l * b);
  }
  public Rectangle(float x){
  p = x;
  q = x;
  }
  public float third(){
  return(p * q);
  }
  public Rectangle(float x, float y){
  p = x;
  q = y;
  }
  public float fourth(){
  return(p * q);
  }
}

Output of the program is given below:

C:\java>java ConstructorOverloading
The area of a rectangle in first constructor is : 8
The area of a rectangle in first constructor is : 25
The area of a rectangle in first constructor is : 4.0
The area of a rectangle in first constructor is : 6.0

Download this program

In this example you learned to define a class with multiple constructor and then use in your program.