In this lesson, you will learn.
The most common way to achieve compile-time polymorphism in Java is through method overloading.
Method overloading allows you to define multiple methods in the same class with the same name but different parameters (either in number, type, or order).
Method overloading enhances code readability and reusability. Instead of creating multiple methods with different names for similar operations, you can use the same name with different parameters to handle various scenarios. This makes your code more organized and easier to understand.
Here are various syntax possibilities to achieve method overloading in Java.

Methods can be overloaded by having a different number of parameters.
void display(int a) {
System.out.println("Got an integer: " + a);
}
void display(int a, int b) {
System.out.println("Got two integers: " + a + ", " + b);
}
Methods can be overloaded by having different types of parameters.
void display(int a) {
System.out.println("Got an integer: " + a);
}
void display(String a) {
System.out.println("Got a string: " + a);
}
If methods have the same number of parameters but in a different order, they are also considered overloaded.
void display(int a, String b) {
System.out.println("Integer: " + a + ", String: " + b);
}
void display(String a, int b) {
System.out.println("String: " + a + ", Integer: " + b);
}
Combining different types and numbers of parameters offers more overloading options.
void display(int a) {
System.out.println("Got an integer: " + a);
}
void display(int a, String b) {
System.out.println("Got an integer "
+ "and a string: " + a + ", " + b);
}
void display(String a, int b, double c) {
System.out.println("Got a string, an integer, "
+ "and a double: " + a + ", " + b + ", " + c);
}
package ch7;
class OverloadingDemo {
void demo() {
System.out.println("No Parameters");
}
void demo(int a) {
System.out.println("a: " + a);
}
void demo(int a, int b) {
System.out.println("a and b: " + a + "," + b);
}
double demo(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class MethodOverloadingEx1 {
public static void main(String args[]) {
OverloadingDemo Obj = new OverloadingDemo();
double result;
Obj.demo();
Obj.demo(10);
Obj.demo(10, 20);
result = Obj.demo(5.5);
System.out.println("Square: "+result);
}
}
No Parameters
a: 10
a and b: 10,20
double a: 5.5
Square: 30.25
package ch7;
public class MethodOverloading {
public void greet() {
System.out.println("Hello!");
}
public void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public void greet(String firstName, String lastName) {
System.out.println("Hello, " + firstName
+ " " + lastName + "!");
}
public static void main(String[] args) {
MethodOverloading example = new MethodOverloading();
example.greet(); // Calls first method
example.greet("John"); // Calls second method
example.greet("John", "Doe"); // Calls third method
}
}
Hello!
Hello, John!
Hello, John Doe!
Imagine you are developing a part of a payment processing system for an e-commerce application. The application needs to handle different types of payments: by cash, by credit card, and by digital wallet. Each payment method requires slightly different information:
Create a PaymentProcessor class that overloads the processPayment method to handle these three types of payments with the appropriate parameters for each.
package ch7;
public class PaymentProcessor {
// Method for processing cash payment
public void processPayment(double amount) {
System.out.println("Processing cash payment of $" + amount);
}
// Method for processing credit card payment
public void processPayment(double amount, String cardNumber,
String expiryDate, int cvv) {
System.out.println("Processing credit card payment of $"
+ amount + " using card number " + cardNumber);
}
// Overloaded method for processing digital wallet
// payment without authorization token
public void processPayment(double amount, String walletID) {
processPayment(amount, walletID, "DEFAULT_TOKEN");
}
// Overloaded method for processing digital wallet
// payment with authorization token
public void processPayment(double amount, String walletID,
String authToken) {
System.out.println("Processing digital wallet payment of $"
+amount+ " using wallet ID " + walletID +
" with auth token " + authToken);
}
public static void main(String[] args) {
PaymentProcessor processor = new PaymentProcessor();
// Cash payment
processor.processPayment(100.0);
// Credit card payment
processor.processPayment(250.0, "1234567890123456", "12/25", 123);
// Digital wallet payment without auth token
processor.processPayment(75.0, "wallet123");
// Digital wallet payment with auth token
processor.processPayment(150.0, "wallet456", "AUTH123");
}
}
Processing cash payment of $100.0
Processing credit card payment of $250.0 using card number 1234567890123456
Processing digital wallet payment of $75.0 using wallet ID wallet123 with
auth token DEFAULT_TOKEN
Processing digital wallet payment of $150.0 using wallet ID wallet456
with auth token AUTH123
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.