Method Overloading means defining multiple methods with the same name but different parameters (number, type, or order). It allows you to perform similar operations in different ways.
Create Project Name: Calculator
Source Code1: Operation.java
public class Operation {
// Method 1: add two integers
public int add(int a, int b) {
return a + b;
}
// Method 2: add three integers
public int add(int a, int b, int c) {
return a + b + c;
}
// Method 3: add two doubles
public double add(double a, double b) {
return a + b;
}
}
Source Code2: Calculator.java
public class Calculator {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Operation calc = new Operation();
System.out.println("add(2, 3): " + calc.add(2, 3)); // Calls int add(int, int)
System.out.println("add(2, 3, 4): " + calc.add(2, 3, 4)); // Calls int add(int, int, int)
System.out.println("add(2.5, 3.5): " + calc.add(2.5, 3.5)); // Calls double add(double, double)
}
}
Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

