Commit b48f970
committed
feat: Add AddOverloadDemo class demonstrating method overloading
WHAT the code does:
- Defines `AddOverloadDemo` with three overloaded `add()` methods:
1. **No parameters** → adds hardcoded numbers (10 + 20) and prints result.
2. **Two parameters** → returns sum of two integers.
3. **Three parameters** → calculates sum of three integers and prints result.
- `main()` demonstrates calling all three variations.
WHY this matters:
- Showcases **method overloading** in Java — same method name, different parameter lists.
- Demonstrates flexibility in designing APIs with multiple usage options.
- Highlights both returning values and printing results.
HOW it works:
1. `obj.add()` → calls no-arg method → prints `"Sum (no parameters): 30"`.
2. `obj.add(15, 25)` → calls two-arg method → returns 40 → printed in main.
3. `obj.add(5, 10, 20)` → calls three-arg method → prints `"Sum (three parameters): 35"`.
Tips & gotchas:
- Overloading is determined at **compile time** (static polymorphism).
- Cannot overload methods by return type alone — parameter list must differ.
- Ensure method signatures are distinct enough to avoid ambiguity.
- Consistency matters: mixing printing vs returning can confuse API users.
Use-cases:
- Educational example for **compile-time polymorphism**.
- Practical for calculator-like utilities with multiple input forms.
- Foundation for designing libraries where methods accept variable arguments.
- Interview prep problem for method overloading.
Short key: class-add overload demo-method-overloading.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2128f89 commit b48f970
1 file changed
+3
-5
lines changedLines changed: 3 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
| 9 | + | |
11 | 10 | | |
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
20 | 19 | | |
21 | | - | |
22 | 20 | | |
23 | 21 | | |
24 | 22 | | |
| |||
0 commit comments