13 Java Fundamentals | Overloaded Methods in Java | By Dummy for Dummies
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. The signature of a method includes the name of the method and its parameters in which they receive arguments. So you can have methods with the same names, yet they will receive different arguments and perform different functions.
METHOD OVERLOADING
Imagine the boss monkey sometimes needs to add 2 numbers, while sometimes it needs to add 3 numbers. But the different number of arguments passed cannot be received by the same method. In that case, you can create another method with the same name and set it to receive the desired number of arguments.
As I said, methods are recognized by signature rather than just names, so even though two methods have the same name, if they receive different numbers or different types (datatype) of arguments, they are considered different, and Java treats 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)); System.out.println("Boss Monkey: Add 3 bananas!"); System.out.println("Result: " + addBananas(2, 3, 4)); System.out.println("Boss Monkey: Add half bananas!"); System.out.println("Result: " + addBananas(2.5, 3.5)); } // 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; } }
POINTS TO REMEMBER
int addBananas(int a, int b) and double addBananas(int a, int b) is not valid overloading. Because the return type is not part of the Signature.add2Bananas, add3Bananas, etc.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); 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(); String word = "for Banana"; char ch = 'B'; String result1 = addBananas(int1, int2); String result2 = addBananas(int3, int4, int5); String result3 = addBananas(double1, double2); String result4 = addBananas(ch, word); 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; } }
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
sendMessagein 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".
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.