Method Overloading in Java [With Examples]
Updated on Mar 07, 2025 | 15 min read | 29.4k views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 07, 2025 | 15 min read | 29.4k views
Share:
Table of Contents
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modular, reusable, flexible, and easy to debug. There are many features of Object-oriented programming, such as inheritance, polymorphism, encapsulation, and abstraction.
In this article, we will discuss method overloading in Java which is the type of polymorphism. The article also contains some unique examples to help you get a better understanding.
Check out our free courses to get an edge over the competition.
Polymorphism means the ability to appear in different forms. It allows the same method to show different behaviors using different methods. There are two types of Polymorphism: Method Overloading and Method Overriding. Method overloading means multiple methods are having the same name but different arguments.
Learn to build applications like Swiggy, Quora, IMDB and more
Method Overriding means the child class can have a method with the same name as the parent class but with a different implementation. We will discuss Method Overloading in more detail in this article. For Method Overriding, you can visit upGrad and get insights.
Check out upGrad’s Java Bootcamp
Method Overloading in Java is one of the most useful features of an Object-Oriented Language. It allows a class to have multiple methods with the same name. The only difference that these methods have is the different list of parameters that are passed through these methods.
It can be understood in simple terms with a simple example. A class addition has two methods named add(), one method has parameters int a and int b, while the other has three integer parameters, i.e., int a, int b, and int c. Therefore, the add() method is said to be overloaded.
Check out upGrad’s Full Stack Development Bootcamp (JS/MERN)
The method which will be executed will depend upon the number of parameters passed in the method calling statement. To illustrate, add(20,30) will call the add() method having two parameters, and add(10,20,30) will call the add method with three parameters.
For example, a class may include two various “add” methods. One of them accepts two double values, i.e., “add(double a double b),” and another accepts the two integer values with the syntax: “add(int a, int b).” This important method overloading example implies that the computer automatically determines which method to call at run-time depending on the variable types passed to the method.
upGrad’s Exclusive Software and Tech Webinar for you –
SAAS Business – What is So Different?
Method overloading in Java can be achieved in different ways. As we have understood that it is the list of parameters that differentiate the two methods with the same name in Java. The different ways of method overloading in Java can be achieved by varying parameters list in one of the below way.
Here are some of the different ways of method overloading in Java, along with example of method overloading in Java:
As it is clear from the name that there will be a different number of parameters in the overloaded methods which will decide which method to execute seeing the method call statement. Below is the method overloading in Java, where the number of parameters varies.
Method1: add(int, int)
Method2: add(int, int, int)
Method calling statement add(20,30) will execute the method1 and Method calling statement add(10,20,30) will execute the method2.
Method Overloading by changing the number of parameters (or arguments):
Multiply() (This method overloading in java example is without parameter)
Multiply(int a, int b) (This overload method multiplies with two parameters)
Multiply(int a, int b, int c) (This overload method multiplies with three parameters)
These three examples change the number of parameters in the Multiply () method. Likewise, you can add more parameters to overload more Multiply() methods in the same class.
In this type of method overloading in Java, the two java methods of a class have the same name, and the number of parameters can be the same or different, but the data type of parameters will differ.
Method1: add(int, int)
Method2: add(int, float)
Method3: add(float, float)
In the above methods, the method name is the same, but the data type of the parameters is different. So, method calling statement add(2, 5.5) will execute the method2. Similarly, add(7.5, 10.25) will execute the method3.
Method Overloading by changing the data type of any single or all parameters:
Multiply(int x, int y) (This method overloading in java example has same data type for both the parameters.)
Multiply(int x, float y) (In this overload method, the second parameter has a different data type.)
Multiply(int x, int y, float z) (In this overload method, the third parameter has a different data type.)
-The last two examples of method overloading show different data types of parameters. You can make such combinations to overload a Java method ‘multiply’.
Get Software Engineering degrees from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
In this type of method overloading in Java, both the method name and the number of parameters is the same, but the difference lies in the sequence of data types of these parameters. Below is the example of overloaded methods:
Method1: add(int, float)
Method2: add(float, int)
Here, calling the statement to add(100, 75.5) will call the method1 to execute, and add(55.25, 150) will execute method2.
Therefore, it is the parameters in the calling statement that decide which overloaded method will be executed.
Read: Method Overloading in Java
In the realm of Java programming, method overloading emerges as a powerful tool, empowering you to enhance code flexibility and achieve code reuse by defining multiple methods within the same class that share the same name but possess distinct parameter lists. This construct explains method overloading in java, allows you to tailor method behavior based on the provided arguments, streamlining development and promoting well-structured code.
Delving into the Mechanism:
The magic of method overloading unfolds at compile time, where the appropriate method variant is selected based on the number and types of arguments you provide when calling the method. This compile-time binding ensures that the correct implementation is invoked, ensuring predictable and reliable code execution.
Crafting a Compelling Example – Real TimeExample of method overloading program in Java:
To illustrate the essence of method overloading, consider the following scenario:
Java
class Animal {
void sound() {
System.out.println(“Animal makes a sound”);
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println(“Dog barks”);
}
void sound(String message) {
System.out.println(“Dog says: ” + message);
}
}
public class Main {
public static void main(String[] args) {
Animal animal = new Dog(); // Upcasting
animal.sound(); // Calls the overridden method in Dog class (no arguments)
Dog dog = new Dog();
dog.sound(); // Calls the overridden method in Dog class (no arguments)
dog.sound(“Hello!”); // Calls the overloaded method with String argument
}
}
In this example, the Dog class extends the Animal class and:
Overrides the sound() method to provide its specific implementation (no arguments).
Overloads the sound() method by adding a new variant that accepts a String argument, allowing more specific behavior.
Enhanced Readability: Overloading allows using meaningful method names that reflect the operation performed, depending on the argument types and count.
Improved Code Organization: It enables grouping related methods within a class, simplifying code structure and making it easier to manage.
Increased Flexibility: Developers can implement methods that perform similar functions but with different input parameters, offering flexibility in method usage.
Code Reuse: By overloading methods, code duplication is minimized, promoting reuse and making the codebase more maintainable.
An integral complement to method overloading is method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass. This technique is pivotal for achieving runtime polymorphism and enhancing flexibility in how objects of different classes are treated. For instance, in a scenario where multiple classes inherit from a common superclass, each subclass can override a generic method defined in the superclass to exhibit behavior that’s specific to the subclass.
Example of Method Overriding in Java:
Consider a class Vehicle with a method move(). A subclass Car can override this method to provide a specific implementation, such as System.out.println(“Car drives on the road”);, while a Boat subclass might implement it as System.out.println(“Boat sails on the water”);. This illustrates how method overriding allows subclasses to tailor inherited methods to their specific needs, ensuring that the correct version of a method is called based on the object’s runtime type, not its compile-time type.
While method overloading offers immense benefits, adhering to key best practices is crucial:
Meaningful Method Names: Opt for descriptive names that elucidate the purpose of each overloaded method, enhancing code readability and maintainability.
Balance over Excess: While overloading promotes flexibility, avoid creating numerous methods with similar functionality. Strive for a balance to prevent code clutter and confusion.
Distinct Parameter Signatures: When designing overloaded methods, carefully consider the types and order of parameters to establish clear and intuitive signatures. This mitigates ambiguity and unexpected behavior.
Comprehensive Documentation: Provide clear and concise documentation for each overloaded method, including details about parameters, return values, and intended use. This aids understanding and reduces potential misuse.
Rigorous Testing: Conduct thorough testing with diverse input values to ensure each overloaded method operates as expected. This guarantees code reliability and robustness.
Now that you have learned the different ways, as well as different method overloading example in Java, let’s take a look at the importance of method overloading in Java.
If the programmer has created a method in Java to perform some action, but later as part of the requirement the programmer wants the same method to act on different parameters, then the coder can simply use the feature of method overloading in Java. This will allow the programmer to create the methods of performing similar functions with the same name so that they do not have to remember the names later.
After understanding what is method overloading in java, the next crucial thing is to know what its need is. Overloading is very useful in Java. It allows you to overload methods as long as the number of parameters (arguments) or type varies for each method. You may want to perform a complex mathematical operation. but occasionally want to do it using two or three numbers. In those cases, function overloading in java proves useful.
If the programmer has created a method in Java to perform some action, but later as part of the requirement the programmer wants the same method to act on different parameters, then the coder can simply use the feature of method overloading in Java. This will allow the programmer to create the methods of performing similar functions with the same name so that they do not have to remember the names later.
In overloading, a class can accept multiple methods with the same name, distinguished by the number and type of arguments passed in the method. A method overloading example can be used to simplify your calls through the method with the minimum possible numbers of arguments whenever all defaults are acceptable. Alternatively, you can also use it when there are multiple ways to identify the same thing, i.e., name (using String data type) and ID (using Long data type).
In the discussed examples, after the overloaded methods achieve initialization, irrespective of the task, it can be done through a common method. Moreover, method overloading affects the methods at the runtime.
When understanding what is method overloading in java, you also need to know what method binding is. Because the method overloading is static polymorphism, the methods are bound in the compilation process.
Methods’ binding occurs at compile time. Hence, many processes like checking or binding are unnecessary during run time.
If you only alter the method’s return, it is not considered method overloading. It leads to ambiguity errors.
Note that the order of arguments, number of arguments, and type of arguments are subsets of the actual method overloading.
Let’s know about the advantages of performing method overloading in Java.
Below are some of the advantages of method overloading in Java:
Below points explain the properties of a method signature in Java
Must Read: Career in Java
It is also important to note that the name of the method and the overloaded methods should be the same.
Mentioned below are some of the top features of method overloading in Java:
Function overloading is mainly used to increase the efficiency of a program and reduce the complexities. It does so by involving more segregated functions that can be used to distinguish between each other according to their individual functionalities. Function overloading in Java basically occurs when functions with the same name can have different parameters passed to them. Overloaded functions are related to static polymorphism or compile time. They can further be used to calculate various mathematical or logical operations that is present within the number of assigned variables in the method.
One of the biggest advantages of function overloading is that you no longer need to create methods that have the same thing as work that is done inside a respective function. This not only provides a solution to the problem of conflicting names but also helps to increase the readability of the program.
Method 1: int Add (float, float)
Method 2: float Add (float, float)
This is the invalid case of method overloading as both the Add() methods have the same name and same list of parameters.
Below is the code that explains the Method Overloading in Java:
Class sum{
private int a;
private int b;
private int c;
private int d;
int public add(int a, int b){
int c;
c=a+b;
return c;
}
int public add(int a, float b){
int c;
c=a+b;
return c;
}
int public add(float a, float b){
int c;
c=a+b;
return c;
}
int public add(float a, int b, int c){
int d;
d=a+b+c;
return d;
}
}
Public static void main (String[]args)
{
// Creating object of the class in main method
sum obj1 = new sum();
sum1=obj1.add(10,20);
sum2=obj1.add(10,55.5);
sum3=obj1.add(110.5,25.5);
sum4=obj1.add(10,20,30);
}
Also Read: Pattern Programs in Java
Till now, we have looked at some of the basic method overloading example in Java, The following examples help you to know what is not considered method overloading in Java.
Example:1
public void multiply(int c, int d)
public void multiply(int m, int n)
Example:2
public void multiply(int a, int b, float c)
public void multiply(int m, int n, float p)
Example:3
public void multiply(int d, float e, int f)
public int multiply(int p, float q, int r)
In this article, the Method Overloading in Java was discussed in detail, explaining its types, rules, and advantages. The topic was explained in-depth with the help of examples. Let us know in the comments if you have any doubt regarding overloading in Java.
If you’re interested to learn more about Java, full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources