13 Java Fundamentals | Overloaded Methods in Java | By Dummy for Dummies

Java Method Overloading | Hassan Bukhari
Java Notes

13 Java Fundamentals | Overloaded Methods in Java | By Dummy for Dummies

Java Beginner Guide Overloading Methods Polymorphism

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:

Java
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;
    }
}
Console Output
Boss Monkey: Add 2 bananas! Result: 5 Boss Monkey: Add 3 bananas! Result: 9 Boss Monkey: Add half bananas! Result: 6.0
⦿ Here each method, when it returns a value, is directly printed inside the println.
⦿ All methods have the same names, yet Java treats them as different methods. It depends on the argument we send to determine which method will be accessed.
⦿ We have three methods which give us three options: either we can send two integers, three integers, or two doubles.
⦿ If we try to send arguments that do not match the parameters, it will throw an error because Java will not find a matching method.
⦿ Even sending arguments in the wrong order will throw an error.

POINTS TO REMEMBER

⦿ Method Overloading means the same method name but a 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 the return type is not part of the Signature.
⦿ Java decides which method to call based on the arguments you pass and the name you mention.
⦿ You can also keep the arguments the same and name the methods differently, and they will act as different methods. That is only done when the inside structure of the code serves a different purpose; if they serve the same purpose, 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, 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.

Java
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;
    }
}
Console Output
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".

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

Console Output
---|| Banana Messenger Results ||--- Single message → Hello Monkey! Repeated message → Hello Monkey! Hello Monkey! Hello Monkey! Weighted message → Message: Fresh Banana | Banana Weight: 2.5 Special message → B for Banana

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.