Java Method Overloading
What is Java Method Overloading?
- Java method overloading allows you to define multiple methods with the same name in the same class, as long as they have different method parameters.
- This allows you to provide multiple methods with the same name, but different behavior based on the arguments passed to the method.
- To understand the Java Method Overloading, Read the Complete Article.
Why method overloading?
- Method overloading in Java provides the following benefits:
- Readability: Overloading methods with the same name but different parameters can make your code more readable and self-documenting. This can improve code maintainability and make it easier for other developers to understand your code.
- Flexibility: Overloading allows you to define methods that can perform similar operations on different data types, which makes your code more flexible and adaptable to different situations.
- Code reusability: Overloading methods reduces the amount of code duplication in your program, as you can reuse method names and their functionality across different parts of your code.
- Compile-time polymorphism: Method overloading is an example of compile-time polymorphism, which allows Java to choose the appropriate method to execute based on the arguments passed to the method. This can improve the performance of your code by reducing the amount of runtime type checking that is necessary.
Method overloading is achieved by:
- Method overloading is achieved by defining two or more methods in a class with the same name, but with different parameter lists. In Java, the parameter list of a method includes the number, type, and order of the method's parameters.
- When a method is called, Java uses the data types of the arguments to determine which version of the method to call. If the number, type, and order of the arguments match the parameters of one of the overloaded methods, that method is called. If there is no method that matches the arguments, a compilation error occurs.
Let’s look at the Java Map OverLoading to perform certain operations.
Example 1: Java Program to Overloading by changing the number of parameters
Run
public class Main { public void sum (int a, int b) { int result = a + b; System.out.println ("Sum of two integers: " + result); } public void sum (int a, int b, int c) { int result = a + b + c; System.out.println ("Sum of three integers: " + result); } public static void main (String[]args) { Main obj = new Main (); // Call sum() with two arguments obj.sum (10, 20); // Call sum() with three arguments obj.sum (10, 20, 30); } }
Output
Sum of two integers: 30 Sum of three integers: 60
Explanation:
In this example, we have defined two methods with the same name sum, but with a different number of parameters.
The first sum method takes two int arguments, while the second sum method takes three int arguments.
In the main method, we create an instance of the Main class and call both sum methods with different arguments.
When we call the sum method with two arguments, the first sum method is called, and when we call the sum method with three arguments, the second sum method is called.
Example 2 : Java Program to Method Overloading by changing the data type of parameters
Run
public class Main { public void display (int num) { System.out.println ("Integer number: " + num); } public void display (double num) { System.out.println ("Double number: " + num); } public static void main (String[]args) { Main obj = new Main (); // Call display() with an int argument obj.display (10); // Call display() with a double argument obj.display (10.5); } }
Output
Integer number: 10 Double number: 10.5
Explanation:
We have defined two methods with the same name display, but with different parameter types. The first display method takes an int argument, while the second display method takes a double argument.
In the main method, we create an instance of the Main class and call both display methods with different arguments.
When we call the display method with an int argument, the first display method is called, and when we call the display method with a double argument, the second display method is called.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment