Java FAQ: Top Questions
8. What is method overloading?
Method overloading is a feature in Java that allows a class to have multiple methods with the same name, but with different parameter lists. The Java compiler distinguishes between these methods based on the number of parameters, their data types, or the order of their data types. This concept is a form of compile-time polymorphism (also known as static polymorphism).
When you call an overloaded method, the compiler determines which specific method to invoke based on the arguments you provide at the time of the call.
- Same method name: All overloaded methods must share the same name.
-
Different parameter list: The key differentiator. This can mean:
- A different number of parameters.
- Different data types of parameters (e.g.,
int
vsdouble
). - A different order of data types of parameters (e.g.,
(int, String)
vs(String, int)
).
- Return type: The return type of overloaded methods can be the same or different. However, changing only the return type is NOT sufficient for method overloading; the parameter list must differ.
-
Access modifiers: Access modifiers (e.g.,
public
,private
) can be different for overloaded methods. - Exception handling: Overloaded methods can declare different exceptions.
Let's look at an example to understand method overloading better:
public class Calculator {
// Method 1: Adds two integers
public int add(int a, int b) {
System.out.println("Calling add(int, int):");
return a + b;
}
// Method 2: Overloaded - Adds three integers
public int add(int a, int b, int c) {
System.out.println("Calling add(int, int, int):");
return a + b + c;
}
// Method 3: Overloaded - Adds two doubles
public double add(double a, double b) {
System.out.println("Calling add(double, double):");
return a + b;
}
// Method 4: Overloaded - Concatenates two strings
public String add(String s1, String s2) {
System.out.println("Calling add(String, String):");
return s1 + s2;
}
// Method 5: Overloaded - Adds an integer and a double (different order of types)
public double add(int a, double b) {
System.out.println("Calling add(int, double):");
return a + b;
}
// Method 6: Overloaded - Adds a double and an integer (different order of types)
public double add(double a, int b) {
System.out.println("Calling add(double, int):");
return a + b;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
// The compiler determines which 'add' method to call based on the arguments
System.out.println("Result 1: " + calc.add(10, 20)); // Calls add(int, int)
System.out.println("Result 2: " + calc.add(10, 20, 30)); // Calls add(int, int, int)
System.out.println("Result 3: " + calc.add(10.5, 20.5)); // Calls add(double, double)
System.out.println("Result 4: " + calc.add("Hello", " World")); // Calls add(String, String)
System.out.println("Result 5: " + calc.add(5, 7.5)); // Calls add(int, double)
System.out.println("Result 6: " + calc.add(7.5, 5)); // Calls add(double, int)
// Note: If you only change the return type, it's NOT overloading:
// public void add(int a, int b) { ... } // This would be a compile-time error if add(int, int) already exists
}
}
Summary Table: Method Overloading
Feature | Description |
---|---|
Concept | Multiple methods in the same class having the same name but different parameter lists. |
Polymorphism Type | Compile-time polymorphism (Static Polymorphism). The method to be called is decided at compile time. |
Method Signature | Same method name, but different number, type, or order of parameters. |
Return Type | Can be the same or different. Changing only the return type is not sufficient for overloading. |
Access Modifiers | Can be different. |
Use Case | To perform similar operations on different types of data or with a varying number of inputs (e.g., print() method in Java, which can print various data types). |