13 Java Fundamentals | Overloaded Methods in Java | By Dummy for Dummies
INTRODUCTION
It is not necessary that different methods must have different names. Methods are actually recognized by signature rather than just name. Signature of method includes name of method and it's parameters in which they recieve arguments. So you can have methods by same names yet they will receive different arguments and perform different functions.
METHOD OVERLOADING
Imagine the boss monkey sometime needs to add 2 numbers, while sometime it needs to add 3 numbers but the different number of arguments passed cannot be recieved by the same method. In that case you can create another method by same name and set it to recieve the desired numbers of arguments.
As I said methods are recognized by signature rather than just names, so even though two methods have same name, if they receive different numbers or different types (datatype) of arguments they are considered different and java treat them as completely different methods. Here is a demonstration:
public class Main {
public static void main(String[] args) {
System.out.println("Boss Monkey: Add 2 bananas!");
System.out.println("Result: " + addBananas(2, 3)); // two arguments
System.out.println("Boss Monkey: Add 3 bananas!");
System.out.println("Result: " + addBananas(2, 3, 4)); // three arguments
System.out.println("Boss Monkey: Add half bananas!");
System.out.println("Result: " + addBananas(2.5, 3.5)); // double arguments
}
// Assistant 1 → adds two integers
public static int addBananas(int a, int b) {
return a + b;
}
// Assistant 2 → adds three integers
public static int addBananas(int a, int b, int c) {
return a + b + c;
}
// Assistant 3 → adds two doubles
public static double addBananas(double a, double b) {
return a + b;
}
}
OUTPUT
EXPLANATION
⦿ Here each method when return a value, it is directly printed inside the println
⦿ All methods have same names yet java treat them as different methods. It depends on the argument we send that which methods will be accessed.
⦿ We have three methods which gives us three options, either we can send two integers, or three integers or two doubles.
⦿ If we try to send arguments that does not match the parameters it will throw error, because java will not find the matching method.
⦿ Even sending arguments in wrong order will throw error e.g. if parameters are set to recieve integer and a double, but we pass argument of double and integer, java will not find matching method and throw error.
POINTS TO REMEMBER
⦿ Method Overloading means same method name, but different parameter list (number, type, or order).
⦿ Return type alone cannot overload a method. For example, int addBananas(int a, int b) and double addBananas(int a, int b) is not valid overloading. Because return type is not part of Signature
⦿ Java decides which method to call based on the arguments you pass and name you mention
⦿ You can also keep the arguments same and name the methods differently, and they will act as different methods. That is only done when inside structure of code serve different purpose, if serve same purpose then doing this is useless.
⦿ If no method matches the given arguments, the program will throw a compile-time error.
⦿ Overloading makes your code more readable and organized, since you don’t have to create separate names like add2Bananas ,
add3Bananas etcc.
EXERCISE
Let's do an exercise for what we studied in this post. Study this code, break it down, analyze it and identify the concepts used here. It would be even better if you write down your observations and make a mini report on it.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Asking the boss monkey for numbers
System.out.print("Boss Monkey: Give me two bananas (integers): ");
int int1 = sc.nextInt();
int int2 = sc.nextInt();
System.out.print("Boss Monkey: Give me three bananas (integers): ");
int int3 = sc.nextInt();
int int4 = sc.nextInt();
int int5 = sc.nextInt();
System.out.print("Boss Monkey: Give me two half bananas (decimals): ");
double double1 = sc.nextDouble();
double double2 = sc.nextDouble();
// Now Boss Monkey gives fixed text and character
String word = "for Banana";
char ch = 'B';
// Calling overloaded methods and storing results
// two integers
String result1 = addBananas(int1, int2);
// three integers
String result2 = addBananas(int3, int4, int5);
// two doubles
String result3 = addBananas(double1, double2);
// char + String
String result4 = addBananas(ch, word);
// Printing the stored results
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
System.out.println(result4);
sc.close();
}
// Assistant 1: adds two integers
public static String addBananas(int a, int b) {
return "Sum of two bananas (" + a + " + " + b + ") = " + (a + b);
}
// Assistant 2: adds three integers
public static String addBananas(int a, int b, int c) {
return "Sum of three bananas (" + a + " + " + b + " + " + c + ") = " + (a + b + c);
}
// Assistant 3: multiplies two doubles
public static String addBananas(double a, double b) {
return "Product of half bananas (" + a + " * " + b + ") = " + (a * b);
}
// Assistant 4: combines char + String
public static String addBananas(char symbol, String text) {
return symbol + " " + text; // Example: B for Banana
}
}
Boss Monkey: Give me two bananas (integers): 2 3
Boss Monkey: Give me three bananas (integers): 4 5 6
Boss Monkey: Give me two half bananas (decimals): 2.5 3.5
Sum of two bananas (2 + 3) = 5
Sum of three bananas (4 + 5 + 6) = 15
Product of half bananas (2.5 * 3.5) = 8.75
B for Banana
MINI PROJECT: BANANA MESSENGER (with Method Overloading)
Boss Monkey wants a messenger system that can send different types of banana messages depending on what information he provides. You will create overloaded methods named sendMessage that behave differently based on the arguments.
Tasks
⦿ Create a class BananaMessenger.
⦿ Overload the method sendMessage in at least 4 different ways:
1. sendMessage(String msg) → returns the message as-is.
2. sendMessage(String msg, int times) → repeats the message multiple times.
3. sendMessage(String msg, double weight) → returns "Message: <msg> | Banana Weight: <weight>".
4. sendMessage(char letter, String word) → returns "B for Banana".
⦿ In the main method:
1. Ask the user for inputs.
2. Call each overloaded method with proper arguments.
3. Store results in variables (result1, result2, etc.).
4. Print them neatly.
Hints
String Concatenation → Use + to join strings.
Repetition → To repeat a string n times, use a for loop and add the string each time.
Return Values → All methods should return a String instead of printing directly.
Store Results → Create variables like String result1 = sendMessage("Hello");
Method Signature → Remember, only the parameters (number, type, order) make methods different. The return type alone does not count.
EXAMPLE OUTPUT
CLOSING
That’s it for method overloading in Java! Overloading lets you use the same method name for different tasks, depending on the arguments you pass. It makes your code cleaner, easier to read, and more flexible.
One name, many behaviors, that’s the power of method overloading.