Java Programming Handbook

    Method Overloading in Java

    In Java, Method Overloading allows us to define multiple methods with the same name but different parameters. It is an example of compile-time polymorphism, where the correct method is selected at compile time based on the method signature.

    Why Use Method Overloading?#

    Method overloading helps in:

    • Improving code readability and reusability
    • Making the code cleaner and more maintainable
    • Reducing redundancy by allowing multiple methods with the same name but different parameters

    Rules for Method Overloading#

    To overload a method in Java, you must follow these rules:

    • Same method name but different parameters (different number, type, or order of parameters)
    • Return type does NOT matter for method overloading (only parameters matter)
    • Methods can have different access modifiers

    1. Method Overloading with Different Number of Parameters#

    We can overload a method by changing the number of parameters.

    Example: Different Number of Parameters#

    class Calculator { // Method with two parameters static int add(int a, int b) { return a + b; } // Overloaded method with three parameters static int add(int a, int b, int c) { return a + b + c; } public static void main(String[] args) { // Calling methods with different arguments System.out.println("Sum of 2 numbers: " + add(10, 20)); System.out.println("Sum of 3 numbers: " + add(10, 20, 30)); } }

    Output:#

    Sum of 2 numbers: 30 Sum of 3 numbers: 60

    Explanation:

    • add(int, int) is called when two arguments are passed.
    • add(int, int, int) is called when three arguments are passed.
    • The correct method is selected based on the number of parameters.

    2. Method Overloading with Different Data Types#

    We can also overload methods by changing the data type of parameters.

    Example: Different Data Types#

    class Display { // Method with an int parameter static void show(int num) { System.out.println("Integer: " + num); } // Overloaded method with a double parameter static void show(double num) { System.out.println("Double: " + num); } public static void main(String[] args) { // Calling methods with different types of arguments show(10); // Calls the int version show(10.5); // Calls the double version } }

    Output:#

    Integer: 10 Double: 10.5

    Explanation:

    • show(int) is called when an integer is passed.
    • show(double) is called when a double is passed.
    • The correct method is selected based on the type of parameter.

    3. Method Overloading with Different Order of Parameters#

    We can overload methods by changing the order of parameters.

    Example: Different Order of Parameters#

    class Printer { // Method with (String, int) static void printDetails(String name, int age) { System.out.println("Name: " + name + ", Age: " + age); } // Overloaded method with (int, String) static void printDetails(int age, String name) { System.out.println("Age: " + age + ", Name: " + name); } public static void main(String[] args) { // Calling methods with different order of parameters printDetails("Alice", 25); printDetails(30, "Bob"); } }

    Output:#

    Name: Alice, Age: 25 Age: 30, Name: Bob

    Explanation:

    • Java considers (String, int) and (int, String) as different method signatures.
    • The correct method is called based on the order of parameters.

    4. Method Overloading with Different Return Types (Not Allowed Alone)#

    In Java, we cannot overload methods by changing only the return type. The compiler does not differentiate methods based on return type alone.

    Example: This Will NOT Work#

    class Example { // Method returning int int add(int a, int b) { return a + b; } // Overloaded method with same parameters but different return type (INVALID) double add(int a, int b) { // Compilation Error return a + b; } }

    Error Message:#

    Error: method add(int,int) is already defined in class Example

    Solution:

    To overload methods, parameters must be different, not just the return type.

    5. Constructor Overloading#

    Just like methods, constructors can also be overloaded. This allows us to create objects with different initial values.

    Example: Constructor Overloading#

    class Student { String name; int age; // Constructor with one parameter Student(String name) { this.name = name; this.age = 18; // Default age } // Overloaded constructor with two parameters Student(String name, int age) { this.name = name; this.age = age; } void display() { System.out.println("Name: " + name + ", Age: " + age); } public static void main(String[] args) { // Creating objects using different constructors Student s1 = new Student("Alice"); Student s2 = new Student("Bob", 22); s1.display(); s2.display(); } }

    Output:#

    Name: Alice, Age: 18 Name: Bob, Age: 22

    Explanation:

    • We created two constructors (Student(String) and Student(String, int)).
    • Depending on the arguments, the correct constructor is selected.

    Key Takeaways#

    Method Overloading allows multiple methods with the same name but different parameters.

    ✅ Methods can be overloaded by changing:

    1. Number of parameters
    2. Data types of parameters
    3. Order of parameters

    Return type alone does NOT differentiate overloaded methods.

    Constructors can also be overloaded to initialize objects differently.

    Conclusion#

    In this blog, we learned:

    • What Method Overloading is and why it is useful
    • Different ways to overload methods in Java
    • How to overload constructors
    • Common mistakes to avoid in method overloading

    Method overloading is a key feature of Java polymorphism that makes code more readable and flexible

    Want to Master Spring Boot and Land Your Dream Job?

    Struggling with coding interviews? Learn Data Structures & Algorithms (DSA) with our expert-led course. Build strong problem-solving skills, write optimized code, and crack top tech interviews with ease

    Learn more

    Last updated on Apr 09, 2025

    Subscribe to our newsletter

    Read articles from Coding Shuttle directly inside your inbox. Subscribe to the newsletter, and don't miss out.