Method Overloading in JavaLast Updated : 2 Jan 2026 Method Overloading in Java allows us to create multiple methods with the same name to perform similar tasks using different parameters. In this chapter, we will learn how method overloading works, its rules, advantages, and practical examples. What is Method Overloading in Java?Method overloading in Java is the feature that enables defining more than one method in a class having the same name but with different types and number of parameters. When a method is called, Java decides which version of it to execute depending on the arguments given. If we have to perform only one operation, having the same name of the methods increases the readability of the Java program. Understanding Method Overloading with ExamplesSuppose you have to perform the addition of the given numbers, but there can be any number of arguments if you write the method such as a(int,int) for two parameters, and b(int,int,int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs. Here's an explanation with real-life examples: 1. Math OperationsIn Math class, you might have multiple methods for adding numbers, each accepting a different number of arguments: Here, the add() method is overloaded to handle both adding two integers and adding three doubles. 2. String ManipulationIn a utility class for string manipulation, you might have overloaded methods for concatenating strings: These methods enable concatenating two or three strings together based on the number of arguments passed. Different Approaches of Method OverloadingMethod overloading in Java can be achieved in the following two ways:
Note: In Java, method overloading is not possible by changing the return type of the method only. Let’s understand each approach to method overloading in detail with syntax and examples. 1. Method Overloading by Changing the Number of ArgumentsMethod overloading in Java allows defining multiple methods with the same name but different parameter lists. One common form of overloading is changing the number of arguments in the method signature. In this example, we have created two methods, the first add() method performs addition of two numbers, and the second add method performs addition of three numbers. In this example, we are creating static methods so that we don't need to create instance for calling methods. Let us take an example to demonstrate the method overloading by changing number of arguments in Java. ExampleCompile and RunOutput: 22 33 2. Method Overloading by Changing Data TypeMethod overloading in Java also allows changing the data type of arguments in the method signature. Here's an example demonstrating method overloading based on the data type of arguments: In this example, we have created two methods that differs in data type. The first add method receives two integer arguments and second add method receives two double arguments. Here, we are going to take an example to demonstrate method overloading by changing the data type of arguments in Java. ExampleCompile and RunOutput: 22 24.9 Return Type Limitation in Method OverloadingMethod overloading in Java is based on the method signature, which includes the method name and parameter list. The return type alone is not sufficient to distinguish between overloaded methods because Java does not consider the return type when resolving method calls. If two methods have the same name and parameter list but different return types, the compiler cannot determine which method to call based solely on the return type. ExampleLet us take an example to demonstrate how the method overloading is not possible by changing the return type of method only in Java. Output:
Main.java:9: error: method add(int,int) is already defined in class Adder
static double add(int a, int b) {
^
1 error
System.out.println(Adder.add(11,11)); //Here, how can Java determine which add() method should be called? Note: Compile Time Error is better than Run Time Error. So, Java compiler renders compiler time error if you declare the same method having same parameters. Method Overloading and the main() MethodThe main() method can be overloaded in Java which is technically correct. But, it won't be considered as the entry point for the Java Virtual Machine (JVM) to start the execution of the program. While overloading the main() method is syntactically valid, it does not serve the purpose of being the entry point for program execution. The JVM expects the standard signature public static void main(String[] args) for the entry point. Any other overloaded main() method will be treated as a regular method and will not be invoked by the JVM to start the program. Therefore, although overloading main() is possible, it's not practically useful for program execution. Let's see a simple example. ExampleCompile and RunOutput: main with String[] Method Overloading and Type PromotionOne type is promoted to another implicitly if no matching datatype is found. Let's understand the concept by the figure given below: ![]() As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int,long,float or double and so on. Example: Method Overloading with TypePromotionLet us take an example to demonstrate the Method Overloading with TypePromotion in Java. ExampleCompile and RunOutput: 40 60 Example: Method Overloading with Type Promotion if Matching FoundIf there are matching type arguments in the method, type promotion is not performed. Let us take an example to demonstrate the method overloading with type promotion if matching found in Java. ExampleCompile and RunOutput: int arg method invoked Example: Method Overloading with Type Promotion in case of AmbiguityIf there are no matching type arguments in the method, and each method promotes similar number of arguments, there will be ambiguity. ExampleOutput: Compile Time Error Note: One type is not de-promoted implicitly for example double cannot be depromoted to any type implicitly.Advantages of Method OverloadingFollowing are the advantages of using method overloading in Java:
Disadvantages of Method OverloadingFollowing are the disadvantages of using method overloading in Java:
Next TopicMethod Overriding in java |
We request you to subscribe our newsletter for upcoming updates.