Java: method overloading
Dec 11, 2023
It is possible to write the same method in java but, we need to make sure the parameter and number of it is different. This thing called method overloading. When we define it clearly, we can choose the method that we used by using the parameter that construct on the method.
Get Aldyputu’s stories in your inbox
Join Medium for free to get updates from this writer.
Lets write the code:
public class Main {
static void sayMyName(){
System.out.println("My name is");
}
static void sayMyName(String firstName){
System.out.println("My name is " + firstName);
}
static void sayMyName(String firstName, String lastName){
System.out.println("My name is " + firstName + " " + lastName);
}
public static void main(String[] args) {
sayMyname();
sayMyname("Putu");
sayMyname("Putu", "Reynaldy");
}
}
/*
Output
My name is
My name is Putu
My name is Putu Reynaldy
Now we can call same function with method overload but always make sure
we understand the parameter needed to call the right method
*/


