Method Overloading
This content is for Java. Switch to the latest version for up-to-date documentation.
Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameter lists.
Basic Method Overloading
Section titled “Basic Method Overloading”public class Calculator { // Method with two int parameters public int add(int a, int b) { return a + b; }
// Method with three int parameters public int add(int a, int b, int c) { return a + b + c; }
// Method with two double parameters public double add(double a, double b) { return a + b; }}How Overloading Works
Section titled “How Overloading Works”Method overloading is resolved at compile time based on:
- Number of parameters
- Types of parameters
- Order of parameters
The Java compiler selects the most appropriate method to call based on the arguments you provide.
Overloading Rules
Section titled “Overloading Rules”- Methods must have the same name
- Methods must have different parameter lists
- Return type alone is not enough to distinguish overloaded methods
- Access modifiers don’t affect method overloading
Valid Overloading Examples
Section titled “Valid Overloading Examples”class Example { void display(int num) { System.out.println("Integer: " + num); }
void display(String text) { System.out.println("String: " + text); }
void display(int num1, int num2) { System.out.println("Two integers: " + num1 + " and " + num2); }
// Different order of parameters void process(int id, String name) { System.out.println("Processing ID: " + id + ", Name: " + name); }
void process(String name, int id) { System.out.println("Processing Name: " + name + ", ID: " + id); }}Invalid Overloading Examples
Section titled “Invalid Overloading Examples”class InvalidExample { // Invalid: Only the return type is different int getValue() { return 10; } // double getValue() { return 10.5; } // Won't compile
// Valid: Different parameter list double getValue(double multiplier) { return 10 * multiplier; }}Automatic Type Promotion
Section titled “Automatic Type Promotion”Java may automatically promote a data type when searching for a matching method:
class TypePromotion { void show(int num) { System.out.println("int method: " + num); }
void show(double num) { System.out.println("double method: " + num); }
public static void main(String[] args) { TypePromotion obj = new TypePromotion();
obj.show(5); // Calls int version obj.show(5.5); // Calls double version obj.show('A'); // Calls int version (char promoted to int) }}Overloading vs. Overriding
Section titled “Overloading vs. Overriding”Don’t confuse overloading with overriding:
- Overloading: Multiple methods with the same name but different parameters in the same class
- Overriding: Replacing a method from a parent class with a new implementation in a child class
Benefits of Method Overloading
Section titled “Benefits of Method Overloading”- Improves code readability and reusability
- Reduces the need for remembering different method names for similar operations
- Allows methods to handle different data types and parameter counts