What is constructor overloading in Java?

Java 8Object Oriented ProgrammingProgramming

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
raja
Updated on 30-Jul-2019 22:30:20

Advertisements