← Back to all posts

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, 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:

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


    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


    Hints


    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.

    ← Previous 12 - Methods in Java Next → 14 - Recursion in Java