Method overloading in Java is a technique that allows a class to have multiple methods with the same name but different parameter lists.
This enables a method to perform different tasks based on the arguments passed to it.
Overloaded methods can have different return types, but they must have a different number or type of parameters.
Readability and Maintainability: Method overloading can improve code readability by allowing methods with similar functionality to be grouped together under the same name, making the code easier to understand. It also enhances code maintainability by reducing redundancy.
Flexibility: It provides flexibility by allowing methods to accept different types or numbers of parameters, catering to different use cases without the need for distinct method names.
Polymorphism: Method overloading contributes to polymorphism, where different methods with the same name can be invoked based on the number or types of arguments passed.
class Calculator { // Method with two `integer` parameters and return data type is `integer` int add(int a, int b) { return a + b; } // Method with three `integer` parameters and return data type is `integer` int add(int a, int b, int c) { return a + b + c; } // Method with two `double` parameters and return data type is `double` double add(double a, double b) { return a + b; } }
Added 3 methods with same name. "add" method is overloaded three times, but different in number of arguments and data types.
The first "add" method takes two integer parameters.
The second "add" method takes three integer parameters.
The third "add" method takes two double parameters.
Java determines which overloaded method to call based on the number and types of arguments passed to the method.
When we call the "add" method, the appropriate version of the method is selected based on the argument types and the number of arguments.
class Calculator { // Method with two `integer` parameters and return data type is `integer` int add(int a, int b) { return a + b; } // Method with three `integer` parameters and return data type is `integer` int add(int a, int b, int c) { return a + b + c; } // Method with two `double` parameters and return data type is `double` double add(double a, double b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calc = new Calculator(); // Calls first add method System.out.println("Sum of 5 and 7 is: " + calc.add(5, 7)); // Output: Sum of 5 and 7 is: 12 //Calls second add method System.out.println("Sum of 5, 5, and 7 is: " + calc.add(5, 5, 7)); // Output: Sum of 5, 3, and 7 is: 17 // Calls third add method System.out.println("Sum of 2.5 and 3.5 is: " + calc.add(2.5, 3.5)); // Output: Sum of 2.5 and 3.5 is: 6.0 } }
Created an instance of Calculator class in Main Class. invoked all 3 methods of Calculator class with the help of calculator instance.
Sum of 5 and 7 is: 12 Sum of 5, 5, and 7 is: 17 Sum of 2.5 and 3.5 is: 6.0
Method overloading provides a way to define multiple methods with the same name, making the code more readable and expressive.
It is commonly used in Java libraries and frameworks to provide multiple ways to interact with objects and perform operations with different argument types.