Java Method Overloading - Rules, Types and Complete Examples

⏱️ 9 min read • Beginner Level • Lesson 35

Lesson 35 of 124 of Java Tutorial

Method overloading in Java means defining multiple methods with the same name in the same class, but with different parameter lists. It helps make code readable and allows one method name to perform similar tasks with different inputs.

Before learning method overloading, you should understand Java Methods and Java Method Parameters. After this lesson, continue with Java Recursion.


What is Method Overloading in Java?

Method overloading allows a class to have more than one method with the same name, but each method must have a different parameter list.

Simple Meaning: Method overloading means using the same method name for similar tasks while changing the parameter list.
OverloadingIdea.java
void add(int a, int b) { }
void add(int a, int b, int c) { }
void add(double a, double b) { }

All three methods are named add, but their parameter lists are different.

Note: Method overloading is an example of compile-time polymorphism because the compiler decides which method to call based on arguments.

Why Use Method Overloading?

Real-World Example of Method Overloading

Method overloading is commonly used when the same operation needs to work with different types or amounts of data.

Example: A calculator application may use multiple add() methods:
  • add(int, int)
  • add(int, int, int)
  • add(double, double)

Users perform the same operation (addition), but with different inputs. Instead of creating methods such as addTwoNumbers(), addThreeNumbers(), and addDecimalNumbers(), Java allows the same method name to be reused.

  • It improves code readability.
  • It allows the same method name for similar operations.
  • It reduces the need to remember many different method names.
  • It makes programs cleaner and easier to maintain.
  • It supports compile-time polymorphism.

Rules of Method Overloading in Java

To overload a method, the method name must be the same, but the parameter list must be different.

Allowed? Overloading Rule Example
Yes Change number of parameters add(int, int), add(int, int, int)
Yes Change data type of parameters add(int, int), add(double, double)
Yes Change order of parameters show(String, int), show(int, String)
No Only changing return type int sum(), double sum()
Note: Parameter names do not affect method overloading. Only the number, type, or order of parameters matters.
Example:
void show(int age) and
void show(int marks)
are not overloaded.
Important: return type does not play any role in method overloading.
How Java Chooses an Overloaded Method:

Method Call ➜ Compiler Checks Method Name ➜ Matches Parameter List ➜ Selects Best Matching Method ➜ Executes Method

Overloading by Number of Parameters

In this type, methods have the same name but different number of parameters.

OverloadByNumber.java
Copy Try Download
public class OverloadByNumber {
    static int add(int a, int b) {
        return a + b;
    }

    static int add(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        System.out.println(add(10, 20));
        System.out.println(add(10, 20, 30));
    }
}

Output:

30
60

Overloading by Data Type of Parameters

In this type, methods have the same name and same number of parameters, but parameter data types are different.

OverloadByType.java
Copy Try Download
public class OverloadByType {
    static int add(int a, int b) {
        return a + b;
    }

    static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(10, 20));
        System.out.println(add(10.5, 20.5));
    }
}

Output:

30
31.0

Overloading by Order of Parameters

Method overloading can also be done by changing the order of parameters when the data types are different.

OverloadByOrder.java
Copy Try Download
public class OverloadByOrder {
    static void show(String name, int age) {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    static void show(int age, String name) {
        System.out.println("Age: " + age + ", Name: " + name);
    }

    public static void main(String[] args) {
        show("Ayan", 20);
        show(22, "Rahul");
    }
}

Output:

Name: Ayan, Age: 20
Age: 22, Name: Rahul

Can We Overload a Method by Return Type Only?

No, Java does not allow method overloading by changing only the return type. The parameter list must be different.

InvalidReturnTypeOverloading.java
public class InvalidReturnTypeOverloading {
    static int getValue() {
        return 10;
    }

    // Error: method getValue() is already defined
    static double getValue() {
        return 10.5;
    }
}
Important: Method signature in Java depends on method name and parameter list, not only the return type.

Method Overloading and Type Promotion

If an exact matching method is not found, Java may promote a smaller data type to a larger compatible data type. For example, int can be promoted to long, float, or double.

TypePromotionExample.java
Copy Try Download
public class TypePromotionExample {
    static void show(double number) {
        System.out.println("double method: " + number);
    }

    public static void main(String[] args) {
        show(10); // int is promoted to double
    }
}

Output:

double method: 10.0
Important: Java always prefers an exact match first. Type promotion is used only when no exact matching overloaded method is found.

Can Static Methods be Overloaded?

Yes, static methods can be overloaded in Java if their parameter lists are different.

StaticMethodOverloading.java
Copy Try Download
public class StaticMethodOverloading {
    static void print(int number) {
        System.out.println("Number: " + number);
    }

    static void print(String text) {
        System.out.println("Text: " + text);
    }

    public static void main(String[] args) {
        print(100);
        print("Java");
    }
}

Output:

Number: 100
Text: Java

Can main() Method be Overloaded in Java?

Yes, the main() method can be overloaded in Java. However, JVM starts execution only from public static void main(String[] args).

MainMethodOverloading.java
Copy Try Download
public class MainMethodOverloading {
    public static void main(String[] args) {
        System.out.println("Original main method");
        main(10);
    }

    public static void main(int number) {
        System.out.println("Overloaded main method: " + number);
    }
}

Output:

Original main method
Overloaded main method: 10

Constructor Overloading in Java

Constructors can also be overloaded. Constructor overloading means creating multiple constructors in the same class with different parameter lists.

ConstructorOverloadingExample.java
Copy Try Download
class Student {
    String name;
    int age;

    Student() {
        name = "Unknown";
        age = 0;
    }

    Student(String studentName) {
        name = studentName;
        age = 18;
    }

    Student(String studentName, int studentAge) {
        name = studentName;
        age = studentAge;
    }

    void showDetails() {
        System.out.println(name + " - " + age);
    }
}

public class ConstructorOverloadingExample {
    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("Ayan");
        Student s3 = new Student("Rahul", 22);

        s1.showDetails();
        s2.showDetails();
        s3.showDetails();
    }
}

Output:

Unknown - 0
Ayan - 18
Rahul - 22

Method Overloading vs Method Overriding

Method Overloading Method Overriding
Same method name with different parameters. Same method signature in parent and child class.
Can happen in the same class. Requires inheritance.
Compile-time polymorphism. Runtime polymorphism.
Return type alone cannot overload a method. Return type must be same or covariant.

Complete Example of Method Overloading

This example shows overloading by number of parameters, data type of parameters, and order of parameters.

MethodOverloadingCompleteExample.java
Copy Try Download
public class MethodOverloadingCompleteExample {

    static int calculate(int a, int b) {
        return a + b;
    }

    static int calculate(int a, int b, int c) { // Overloaded by number of parameters
        return a + b + c;
    }

    static double calculate(double a, double b) { // Overloaded by data type
        return a + b;
    }

    static void calculate(String operation, int number) { 
        System.out.println(operation + ": " + number);
    }

    static void calculate(int number, String operation) { // Overloaded by order
        System.out.println(number + " - " + operation);
    }

    public static void main(String[] args) {
        System.out.println(calculate(10, 20));
        System.out.println(calculate(10, 20, 30));
        System.out.println(calculate(10.5, 20.5));
        calculate("Total", 100);
        calculate(200, "Marks");
    }
}

Output:

30
60
31.0
Total: 100
200 - Marks

Explanation

  1. The first method overloads by number of parameters.
  2. The second method overloads by parameter data type.
  3. The third method overloads by parameter order.
  4. All methods have the same name calculate().
  5. The compiler selects the correct method based on the arguments passed.

Common Mistakes in Method Overloading

  • Trying to overload a method by changing only the return type.
  • Using the same parameter list with different variable names only.
  • Confusing method overloading with method overriding.
  • Creating ambiguous method calls with similar parameter types.
  • Forgetting that overloaded methods must have the same method name.
Summary:
  • Method overloading means using the same method name with different parameter lists.
  • Overloading can be done by changing number, type, or order of parameters.
  • Method overloading is compile-time polymorphism.
  • Changing only the return type does not overload a method.
  • Static methods and main methods can be overloaded.
  • Constructors can also be overloaded.

Frequently Asked Questions

Method overloading in Java means defining multiple methods with the same name in the same class, but with different parameter lists.

Method overloading is used to improve code readability, reuse the same method name for similar operations, and make programs easier to maintain.

Yes, method overloading is an example of compile-time polymorphism because the compiler decides which overloaded method to call based on the arguments.

A method can be overloaded by changing the number of parameters, changing the data type of parameters, or changing the order of parameters.

No, Java does not allow method overloading by changing only the return type. The parameter list must be different.

Yes, static methods can be overloaded in Java if their parameter lists are different.

Yes, the main method can be overloaded in Java, but JVM starts execution only from public static void main(String[] args).

Method overloading by number of parameters means creating methods with the same name but different numbers of parameters.

Method overloading by data type means creating methods with the same name and same number of parameters, but with different parameter data types.

Method overloading by order of parameters means changing the order of parameters with different data types, such as show(String, int) and show(int, String).

Type promotion in method overloading means Java may convert a smaller compatible data type to a larger data type if an exact matching method is not found.

Yes, constructors can be overloaded in Java by creating multiple constructors with different parameter lists in the same class.

Method overloading means same method name with different parameters in the same class, while method overriding means redefining a parent class method in a child class with the same signature.

No, method overloading does not require inheritance. It can happen in the same class.

A common mistake is trying to overload methods by changing only the return type. Java requires a different parameter list for overloading.

Next step: Learn Java Recursion

🚀 Continue to Java Recursion →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Method-overloading | Language: Java

Q1. Method overloading is an example of which polymorphism?
Q2. Method overloading is resolved at which time?
Q3. Which example is invalid method overloading?
Q4. What is the main difference between method overloading and method overriding?
Q5. Which is a common mistake in method overloading?
Q6. Which of the following can be used to overload a method?
Q7. What is method overloading in Java?
Q8. Which pair shows valid method overloading?
Q9. Can a method be overloaded by changing only its return type?
Q10. Can the main method be overloaded in Java?
Q11. What is type promotion in method overloading?
Q12. What will be the output of this code?
public class Test {
    static int add(int a, int b) {
        return a + b;
    }

    static int add(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        System.out.println(add(10, 20));
    }
}
Q13. Which option shows overloading by data type of parameters?
Q14. What is constructor overloading?
Q15. Which overloaded method is called by the compiler?
Q16. What will be the output of this code?
public class Test {
    static void show(double number) {
        System.out.println(number);
    }

    public static void main(String[] args) {
        show(10);
    }
}
Q17. Does method overloading require inheritance?
Q18. What will be the output of this code?
public class Test {
    static int add(int a, int b) {
        return a + b;
    }

    static double add(double a, double b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(10.5, 20.5));
    }
}
Q19. Which option shows overloading by order of parameters?
Q20. Can static methods be overloaded in Java?

🎉 Great job! Continue learning Java step by step.