Method Overloading in Java [With Examples]

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. 


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.

Method Overloading in Java

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.

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.

Different Ways to Overload a Method

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:

  1. Number of parameters
  2. The data type of parameters
  3. The sequence of Data type of parameters

The Number of Parameters

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.

The Data Type of Parameters

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.

The Sequence of the Data Type of Parameters

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

Why Do We Need 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.

Let’s know about the advantages of performing method overloading in Java.

Advantages of Performing Method Overloading in Java

Below are some of the advantages of method overloading in Java:

  • It improves the readability of the written code.
  • It adds cleanliness to the code written.
  • It can be used on constructors also so that we can create different objects by passing different data.
  • It gives the programmers the flexibility of calling different methods with similar names.
  • Overloaded methods can have different return types.

Must Read: Career in Java

Rules for Method Overloading in Java

  • The first rule is to change the method signature. Method signature refers to the number of arguments, the sequence of parameters, and the data type of parameters.
  • If we only change the return type of the method keeping the method name and arguments the same, then this will not be the method overloading but will give rise to a compilation error.

Important Points about Method Overloading in Java

  • Any code written in Java cannot have methods with the same name. If we create 2 methods with the same name in Java, it will throw a compilation error.
  • This can be achieved through method overloading. Method overloading allows programmers to write two or more than two methods with the same name, but different parameter lists.
  • Method overloading in Java is similar to constructor overloading. The only difference is method overloading occurs for the methods defined in the class while constructor overloading is for the constructor methods of the class.
  • In method overloading in Java, the overloaded methods have a different list of parameters. It has nothing to do with the return type of the method. As an example; if two methods are having the same name and same list of parameters, but different return types, then this is not method overloading in Java, but is an invalid case.

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;


                               return c;


               int public add(int a, float b){

                               int c;


                               return c;


               int public add(float a, float b){

                               int c;


                               return c;


               int public add(float a, int b, int c){

                               int d;


                               return d;




Public static void main (String[]args)


               // Creating object of the class in main method

               sum obj1 = new sum();






Also Read: Pattern Programs in Java


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.

What are the contrasts between C++ and Java?

C++ is a platform-specific programming language, whereas Java is a platform-independent language. C++ is a computer language that is mainly used for system programming. Java is a programming language that is extensively used in desktop, web-based, business, and mobile applications. C++ was created to program systems and applications. It was a C programming language extension. Java was initially intended as a printer interpreter, but it was eventually expanded to include network computing capability. It was created with the intention of being simple to use and accessible to a broader audience. Both C++ and Java programming languages are object-oriented programming languages. However, in the C language, a single root hierarchy is not viable, and everything in Java except fundamental types is an object.

What are the roles and responsibilities of Java professionals?

As a Java developer, engineer, or other java experts, you'll be responsible for designing, implementing, and maintaining Java applications with high throughput and low latency, which are necessary for mission-critical systems. You'll be expected to provide high availability and performance while also participating in all stages of the development lifecycle. As a Java professional, you'll be responsible for writing well-designed, efficient, and tested code, as well as conducting software analysis, programming, testing, and debugging.

What are the benefits of Java?

Java has a number of features that set it apart from other languages and environments, making it ideal for almost every programming task. To begin with, Java is simple to learn because it was created to be simple to use, making it easier to write, build, debug, and learn than other programming languages. Second, it is object-oriented, allowing you to write modular applications and code that can be reused. Finally, Java is platform-agnostic, which is one of its most major advantages since it can simply shift from one computer system to another, something that other programming languages can't accomplish. Java has become a language of choice for offering multinational Internet solutions because of its durability, simplicity of use, cross-platform flexibility, and security features.

Want to share this article?

Land on Your Dream Job

Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks