If class having more than one method with same name but there is change in signature then it is called Method Overloading.
Change in Signature refers to any of the following bullet points.
- Change in numbers of parameters method accept when it get invoked.
- Change in sequence of parameters passed.
- Change in the parameters datatype.
Real World Example
Here i will demonstrate method overloading with help of online shopping portal. These days people using it more frequently for purchasing any items such as clothes, groceries etc.
public class ShoppingPortal
{
public void makePayment(String username, String password){
//Making Payment through Internet Banking
}
public void makePayment(long mobileNumber, int password){
//Making Payment through any UPI support App(Paytm, Google Pay etc)
}
public void makePayment(long cardNumber, int cvv, String name,
String expireDate){
//Making Payment through Credit or Debit Card
}
// method calling statements
makePayment("sk123", "787@3");
makePayment(09564123587, 123546);
makePayment(1233344322233, 444, "john", "21/08/2022");
}
In above example , i have considered a class ShoppingPortal which has total three methods with same name makePayment(). The bussiness logic of this method is to perform the payment transcations.
Payment can be done either of following modes.
- Through Internet Banking.
- Using UPI Support Application.
- With Credit Card or Debit Card .
Here one thing is very common, while doing the payment from any one of the mode , same method will get invoked makePayment() , but we have one problem , class contains the total 3 methods with same name, then which method will get executed when it get invoked.
Don’t worry the method execution will completly depends upon the numbers of parameter we passed while calling it. Below i explained it in details.
Scenario #1 : Payment through Internet Banking
In this scenario , I am doing a payment through internet banking . In this case it obivous that it requires my credentials such as username and password. Hence once I login-in and proceed for the payment it automatically considered the bank details with is linked with my username and password.
Thus, makePayment() will invoked along with two parameters and the first method inside the class will get executed.
public void makePayment(String username, String password){
//Making Payment through Internet Banking
}
makePayment("sk123", "787@3"); // method calling statement
Scenario #2 : Payment through UPI Support Application
In this scenario i’m doing the payment using any of the UPI Supported Application such as Google Pay, or PhonePay etc.
Since we already know that for making any payment through this mode, it requires a Phone number or Mobile number. This mobile number contains all necessary details of the user.
When i’m doing payment , it requires two essential data , that is mobile number and password.
Once i completed payment through this mode the makePayment() method will get called and since the datatype of mobile number is long and for password it is String , so the second method of the class will get executed.
public void makePayment(long mobileNumber, int password){
//Making Payment through any UPI support App(Paytm, Google Pay etc)
}
makePayment(09564123587, 123546); //method calling statement
Scenario #3 : Payment through Credit or Debit Card
In this payment mode , usually we required total of four data such as card number, cvv , name and card expire date.
Once i enter all four metioned values then method makePayment() will get invoked along with that parameters. Since there are total 4 parameters i’m passing, so it will mapped with the last method and their business logic will get executed.
public void makePayment(long cardNumber, int cvv, String name,
String expireDate){
//Making Payment through Credit or Debit Card
}
makePayment(1233344322233, 444, "john", "21/08/2022");
Predefined Method in Java which implement Method Overloading
In PrintStream Class there are total 10 overloaded methods named println() is present. Below is the sample code snippets of class PrintStream.
class PrintStream{
public void println(){
//business logic
}
public void println(boolean x){
//business logic
}
public void println(float x){
//business logic
}
..........
..........
}
Following are the overloaded method in PrintStream Class
type | method | description |
void | println() | no argument required. |
void | println(boolean x) | accept and print boolean value. |
void | println(char x) | accept and print character value. |
void | println(int x) | accept and print int value. |
void | println(long x) | accept and print long value. |
void | println(float x) | accept and print float or decimal value. |
void | println(double x) | accept and print double value. |
void | println(String x) | accept and print string value. |
void | println(Object x) | accept and print Object address. |
void | println(char[] x) | accept and print array of characters. |
Advantages or Purpose of Method Overloading
- It increases the code Readibility.
- It used to achieve Compile Time Polymorphism.
Thank you for reading if you have any doubt , please feel free to write in comment box below , i’ll make sure all your queries to answered as soon as possible.