/**
* @author lautturi.com
* Java example: Java Constructor Overloading Example
*/
import java.util.*;
class Person {
String name;
// constructor with no parameter
Person() {
this.name = "Lautturi";
}
// constructor with a single parameter
Person(String name) {
this.name = name;
}
public void getName() {
System.out.println("Name: " + this.name);
}
}
public class Lautturi {
public static void main(String[] args) {
// call constructor with no parameter
Person obj1 = new Person();
// call constructor with a single parameter
Person obj2 = new Person("Tom");
obj1.getName();
obj2.getName();
}
}
output:
Name: Lautturi Name: Tom