Overloaded methods have the same name but different number or type of parameters.
In this example Person constructor (method) is overloaded since two constructors have the same name but different
number and type of parameters.
Overloading
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String[] args) {
Person john = new Person("John");
john.say();
Person jill = new Person("Jill", 25);
jill.say();
}
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person {
//PROPERTIES.
String name;
int age;
//CONSTRUCTOR WITH ONE PARAMETER.
Person(String name) {
this.name = name;
}
//CONSTRUCTOR WITH TWO PARAMETERS.
Person(String name, int age) {
this.name = name;
this.age = age;
}
//METHOD.
void say() {
System.out.println(name + " is " + age);
}
}