By Dummy for Dummies
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.
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, not just name. So even if two methods share a name, receiving a different number or type of arguments makes them completely different methods as far as Java is concerned. Here's 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; } }
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.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; } }
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.
sendMessage in at least 4 different ways:sendMessage(String msg) returns the message as-is.sendMessage(String msg, int times) repeats the message multiple times.sendMessage(String msg, double weight) returns "Message: <msg> | Banana Weight: <weight>".sendMessage(char letter, String word) returns "B for Banana".String result1 = sendMessage("Hello");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.