Commit 4ef0c53
committed
feat: Add SimpleCalculator class demonstrating method overloading
WHAT the code does:
- Defines a `SimpleCalculator` class with three overloaded `calculate()` methods:
1. **No parameters** → adds two constants (`7 + 13 = 20`) and prints the result.
2. **Two parameters** → multiplies two integers and returns the product.
3. **Three parameters** → subtracts two numbers from the first and prints the result.
- `main()` demonstrates all three variations:
- Calls no-arg method → prints addition.
- Calls two-arg method → prints multiplication.
- Calls three-arg method → prints subtraction.
WHY this matters:
- Demonstrates **method overloading** (same name, different signatures).
- Shows multiple operations (addition, multiplication, subtraction) under a unified method name.
- Reinforces compile-time polymorphism in Java.
HOW it works:
1. `calc.calculate()` → prints `"Addition (no parameters): 20"`.
2. `calc.calculate(4, 5)` → returns `20`, printed as multiplication.
3. `calc.calculate(50, 20, 10)` → computes `20`, prints subtraction result.
Method Signature Notes:
- Java resolves overloading by the **number and type of parameters** at compile time.
- Return type does not affect method selection.
Tips & gotchas:
- Having a method name that does different operations (`add`, `multiply`, `subtract`) under `calculate` can confuse readability.
- For clarity, separate methods with descriptive names may be better in production.
- Method overloading is useful when operations are **similar in purpose**, not unrelated.
Use-cases:
- Educational example for compile-time polymorphism.
- Foundation for building simple calculators or utility classes.
- Interview prep for method overloading and signatures.
Short key: class-simple calculator-method-overloading.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 99f8e86 commit 4ef0c53
1 file changed
+4
-7
lines changedLines changed: 4 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 2 | + | |
| 3 | + | |
6 | 4 | | |
7 | 5 | | |
8 | 6 | | |
9 | 7 | | |
10 | 8 | | |
11 | | - | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
16 | | - | |
| 14 | + | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
20 | 18 | | |
21 | 19 | | |
22 | 20 | | |
23 | 21 | | |
24 | | - | |
25 | 22 | | |
26 | 23 | | |
27 | 24 | | |
| |||
0 commit comments