program to show constructor calling in multi-constructor

public class Multiconstructor  //to execute more than one const on creation of single object
{
public Multiconstructor(int args) //constructor having same class name and a return type
{
System.out.println("Called integer parameter: " + args);
}
public Multiconstructor(String arg) {
System.out.println("Called string parameter: " + arg);
}
public static void main(String args[]) {
Scanner define = new Scanner(System.in);
System.out.println("string parameter");
String s1 = define.nextLine();
System.out.println("integer parameter");
int s2 = define.nextInt();
Multiconstructor m1 = new Multiconstructor(s1); // constuuctor calling
Multiconstructor m2 = new Multiconstructor(s2);
}
}

OUTPUT

string parameter
shashi
integer parameter
7277
Called string parameter: shashi
Called integer parameter: 7277

No comments:

Post a Comment