What is constructor overloading in Java?


Similar to methods you can also overload constructors i.e. you can write more than constructor with different parameters.

And, based on the parameters we pass at the time of instantiation, respective constructor will be invoked.

Example

Live Demo

public class Sample {
   public Sample() {
      System.out.println("Hello how are you");
   }

   public Sample(String data) {
      System.out.println(data);
   }
   public static void main(String args[]){
      Sample obj = new Sample("Tutorialspoint");
   }
}

Output

Tutorialspoint

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

529 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements