What is Overloading in Java ?

Video Tutorial
FREE
Method overloading thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

What is Overloading in Java ?

In Java, it is possible to create methods that have the same name, but different parameter lists and different definitions. This is called function overloading. Operator overloading is the ability of an operator to redefine its functionality.

In Java, "operator overloading is not supported". In method overloading, we can create methods having the same name but differ in the type, number, and/or sequence of parameters.

When an overloaded method is invoked, Java uses the type, number, and/or, sequence, or arguments as its guide to determine which version of the overloaded method to call.

When Java encounters a call to a method that is overloaded, it simply executes the version of the method whose parameters match the arguments used in the call.

Rules for Method Overloading :

  • The overloaded method must change the argument list (number of parameters, data type, or sequence of parameters).
  • The overloaded method can change the return type.
  • The overloaded method can change the access modifier (the signature of the function should be different).

Why is Overloading Used in Java ?

It is used when different objects are required to perform a similar set of tasks but use different input parameters.

For instance, in an e-commerce platform, there are different modes of payment options like COD and UPI etc, all of which are overloaded to perform a single function.

When we call the overloaded method in an object, Java matches up the method name initially and then the method decides the number and type of parameters of the definitions to be executed.

Example of Overloading in Java

To create an overloaded method, all we have to do is to provide several different method definitions in the class, all with the same name, but different parameters. The difference may either be in the number or type of arguments. That is, each parameter list should be unique.

Note :
The method’s return type does not play any role in this.

Here’s an example of creating an overloaded method :

Example - 1 :

Understanding overloading by calculating the area of two objects, circle and rectangle.

Output :

In the given example, the area method is overloaded by changing the number of parameters, keeping the same return type as well as a data type of parameter.


Example - 2 :

Understanding overloading by changing parameters in the argument.

Output :

In this example, the number of parameters as well as its data type is changed to overload the method. If the parameters provided are both int type variables then the first sum method is executed. If there are three parameters, all int then the second method is invoked. In case two double data type parameters are given then the third method is invoked.


Example - 3 :

Output :

In this example, there are two prog methods with different parameters and return types. The code showcases how method overloading allows us to invoke the appropriate method based on the number and types of arguments provided, resulting in different outputs.

Learn More

Overloading vs Overriding in Java

Conclusion

  • In Java, it is possible to create methods that have the same name, but different parameter lists and different definitions. This is called overloading.
  • Rules for Method Overloading :
    • The overloaded method must change the argument list (number of parameters, data type, or sequence of parameters).
    • The overloaded method can change the return type.
    • The overloaded method can change the access modifier.
  • We cannot implement method overloading solely by changing the return type or access modifier of a method. Method overloading is determined based on the method signature.
  • Overloading is used when objects are required to perform similar tasks but use different input parameters.