What is constructor overloading ?

Like method overloading we can overload constructors also. We can have multiple constructors in a class. Along with default constructor, we can have constructors with parameters. The no of parameters can be same, and it can have different datatypes. Below example gives sample code for constructors overloading.Following is an Example:

package com.test.constructors;

public class OverloadingExample {
     
    public OverloadingExample (){
        System.out.println("Inside default constructor");
    }
    public OverloadingExample (int i){
        System.out.println("Inside single parameter constructor with int value");
    }
    public OverloadingExample (String str){
        System.out.println("Inside single parameter constructor with String object");
    }
    public OverloadingExample (int i, int j){
        System.out.println("Inside double parameter constructor");
    }
     
    public static void main(String a[]){
        OverloadingExample default   =   new OverloadingExample ();
        OverloadingExample oneParam  = new OverloadingExample (10);
        OverloadingExample twoParam  = new OverloadingExample (10,20);
        OverloadingExample threeParam= new OverloadingExample ("NinjaAD");
    }
}

 

CORE JAVA CONSTRUCTOR