Java Method Overloading
Java Method Overloading
Method Overloading in Java allows multiple methods in the same class to have the same name but different parameters.
It is a compile-time polymorphism technique.
1. Rules of Method Overloading
Methods must have the same name.
Methods must have different parameters:
Different number of parameters, or
Different type of parameters, or
Both
Return type alone cannot distinguish overloaded methods.
Methods can have different access modifiers.
2. Example 1: Different Number of Parameters
3. Example 2: Different Parameter Types
4. Example 3: Mixed Parameters
5. Why Use Method Overloading?
Improves code readability.
Avoids writing multiple method names for similar tasks.
Enables flexible methods to handle different data types and arguments.
🧠 Key Points
| Feature | Description |
|---|---|
| Method Name | Same for all overloaded methods |
| Parameters | Must differ in type, number, or both |
| Return Type | Can be different but cannot alone define overloading |
| Compile-Time Polymorphism | Overloading is resolved during compilation |
