Commit 6db4804
committed
feat: Add DemonstratesMethodOverloading class with multiple add() methods
WHAT the code does:
- Defines `DemonstratesMethodOverloading` with three overloaded `add()` methods:
1. `add()` → no parameters → adds hardcoded values `3 + 5` and prints result.
2. `add(int x, int y)` → takes two integers, returns their sum.
3. `add(int x, int y, int z)` → takes three integers, computes and prints their sum.
- `main()` demonstrates:
- Calling `add()` with no arguments.
- Calling `add(int, int, int)` with three integers.
- Calling `add(int, int)` with two integers and printing the returned result.
WHY this matters:
- Demonstrates **method overloading** in Java (compile-time polymorphism).
- Shows that multiple methods can share the same name if they differ in parameter count or type.
- Highlights both printing and returning results for flexibility.
HOW it works:
1. `obj.add()` → calls no-arg method → prints `8`.
2. `obj.add(12, 45, 67)` → calls three-arg method → prints `124`.
3. `obj.add(100, 45)` → calls two-arg method → returns `145` → printed in `main`.
Method Signature:
- Java uses **number and type of parameters** to decide which method to invoke.
- `add()` → no parameters → calls the first method.
- `add(int x, int y)` → exactly two integers → calls the second method.
- `add(int x, int y, int z)` → three integers → calls the third method.
Compile-time Resolution:
- Method choice is made at compile time based on the **method signature**.
Return Type is Ignored:
- Java does not consider return type when overloading — only parameters matter.
✅ Summary:
- Method overloading allows multiple methods with the same name but different parameters.
- Useful for providing **flexible ways to perform similar operations** without inventing new method names.
Use-cases:
- Calculator utilities with multiple input forms.
- Simplifying APIs by grouping related operations.
- Educational demonstration of compile-time polymorphism.
Short key: class-methodover loading-add-demo.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 50e66a1 commit 6db4804
File tree
1 file changed
+20
-24
lines changed- Section10Methods/PracticeProblems/src
1 file changed
+20
-24
lines changedLines changed: 20 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
| 1 | + | |
| 2 | + | |
| 3 | + | |
17 | 4 | | |
18 | 5 | | |
19 | 6 | | |
20 | 7 | | |
21 | 8 | | |
22 | 9 | | |
23 | 10 | | |
24 | | - | |
25 | | - | |
| 11 | + | |
| 12 | + | |
26 | 13 | | |
27 | 14 | | |
28 | 15 | | |
29 | 16 | | |
30 | 17 | | |
31 | | - | |
32 | | - | |
| 18 | + | |
| 19 | + | |
33 | 20 | | |
34 | 21 | | |
35 | 22 | | |
36 | 23 | | |
37 | 24 | | |
38 | | - | |
39 | | - | |
| 25 | + | |
40 | 26 | | |
41 | | - | |
42 | 27 | | |
| 28 | + | |
43 | 29 | | |
44 | 30 | | |
45 | | - | |
46 | 31 | | |
47 | 32 | | |
48 | 33 | | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
0 commit comments