
In Java, method overloading happens when the names of two methods are the same but the arguments are different. Two methods with the same name but different parameters are permitted in Java, and these methods are referred to as overloaded methods.
We must verify method names, which must be the same, and parameters, which must be different, when overloading methods. All other factors, such as return type, throws clause, access modifier, and so on, are not required to be checked.
Table of Contents
Advantages of Method Overloading in Java
- The readability of the program is improved by method overloading.
- Because the binding is done during the compilation process, the execution time is reduced.
- This gives programmers more freedom since they may use the same approach for different kinds of data.
- Overloading methods reduces the code’s complexity and allow us to reuse the code, which saves memory.
How do you overload a method in Java?
In Java, there are two ways to overload a method.
- Number of Arguments: By changing the number of method arguments
- Argument Data Types: By changing the data types of method arguments
By changing the number of method arguments
Here’s an example in which we’ve constructed two methods that will perform number addition. The first method will perform a two-number addition, whereas the second method will perform a three-number addition.
public class MethodOverloadingDemo { public void addition(int num1, int num2) { System.out.println("Addition of two numbers: " + (num1 + num2)); } public void addition(int num1, int num2, int num3) { System.out.println("Addition of three numbers: " + (num1 + num2 + num3)); } public static void main(String[] args) { MethodOverloadingDemo obj = new MethodOverloadingDemo(); obj.addition(10, 20); obj.addition(10, 20, 30); } }
Output: Addition of two numbers: 30 Addition of three numbers: 60
By changing the data types of method arguments
Here’s another example, in which we’ve constructed two techniques for performing number addition. The addition of two integer type data will be performed by the first method, while the addition of two double type data will be performed by the second method.
public class MethodOverloadingDemo { public void addition(int num1, int num2) { System.out.println("Addition of int numbers: " + (num1 + num2)); } public void addition(double num1, double num2) { System.out.println("Addition of double types: " + (num1 + num2)); } public static void main(String[] args) { MethodOverloadingDemo obj = new MethodOverloadingDemo(); obj.addition(10, 20); obj.addition(10.20, 20.30); } }
Output: Addition of int numbers: 30 Addition of double types: 30.5
Overloading main method in Java
Can we overload main method in Java? Yes, we can overload main method in Java. This is the most important topic of Java. It’s also a popular question in the interview room.
In Java, we can overload the main() method with different numbers and types of parameters, but the JVM only understands the main() method, which has just one “String[]” type argument.
public class MainMethodOverloading { public static void main(int args) { System.out.println("Integer type argument"); System.out.println(args); } public static void main(char args) { System.out.println("Character type argument"); System.out.println(args); } public static void main(Double[] args) { System.out.println("Double Array Type"); System.out.println(args); } public static void main(String[] args) { System.out.println("Original main method"); MainMethodOverloading.main(100); MainMethodOverloading.main('A'); MainMethodOverloading.main(1000); MainMethodOverloading.main(new Double[]{10.25}); } }
Output: Original main method Integer type argument 100 Character type argument A Integer type argument 1000 Double Array Type [Ljava.lang.Double;@2a139a55
Method Overloading in Java real time example
Any ecommerce website’s payment choices include netbanking, COD, credit card, and so on. This means that a payment mechanism is overloaded several times in order to accomplish a single payment function in multiple ways.
public class Payment { public void getPayment() { System.out.println("Cash on Delivery"); } public void getPayment(String netBankingId, String Password) { System.out.println("Payment by using netbanking"); } public void getPayment(String name, String creditCarNo, String expiryDate, int cvvNo) { System.out.println("Payment by using Credit card"); } public static void main(String[] args) { Payment payment = new Payment(); payment.getPayment(); payment.getPayment("2302323", "Geek202*"); payment.getPayment("Geek Kumar", "3288-2113-2003-0000", "10/10/2022", 000); } }
Output: Cash on Delivery Payment by using netbanking Payment by using Credit card
Frequently Asked Questions – Method Overloading in Java
- What is method overloading in Java?
- What is method overloading and overriding in Java?
- When two methods have the same name but different parameters, what is it called?
- What are override methods?
- What happens if two methods have the same name, the same parameters, but return types that aren’t the same?
- Is it possible to have two or more methods with the same name but different parameter lists?