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

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.

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.

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, 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;

                               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

Conclusion

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. 

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.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

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

×