Skip to content

Commit 6db4804

Browse files
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

1 file changed

+20
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,44 @@
1-
/*
2-
Can you write a Java class from scratch that demonstrates method overloading for an add() method?
3-
4-
The class should include:
5-
6-
A method add() with no parameters that adds two hardcoded numbers and prints the result.
7-
A method add(int x, int y) that returns the sum of two integers.
8-
A method add(int x, int y, int z) that prints the sum of three integers.
9-
A main method where you create an object of the class and call all three overloaded methods with sample inputs.
10-
Bonus: Explain how Java determines which overloaded method to call.
11-
*/
12-
13-
class HardCodes11
14-
{
15-
void add() //No Parameters.
16-
{
1+
class DemonstratesMethodOverloading {
2+
//No Parameters.
3+
void add() {
174
int a, b, c;
185
a=3;
196
b=5;
207
c=a+b;
218
System.out.println(c);
229
}
2310

24-
int add(int x, int y) //Two Parameters.
25-
{
11+
//Two Parameters.
12+
int add(int x, int y) {
2613
int z;
2714
z=x+y;
2815
return z;
2916
}
3017

31-
void add(int x, int y, int z) //Three Parameters.
32-
{
18+
//Three Parameters.
19+
void add(int x, int y, int z) {
3320
int m = x+y+z;
3421
System.out.println(m);
3522
}
3623

3724
public static void main(String[] args) {
38-
HardCodes11 obj = new HardCodes11();
39-
25+
DemonstratesMethodOverloading obj = new DemonstratesMethodOverloading();
4026
obj.add();
41-
int num;
4227

28+
int num;
4329
obj.add(12,45,67);
4430
num = obj.add(100,45);
45-
4631
System.out.println(num);
4732
}
4833
}
34+
35+
/*
36+
Can you write a Java class from scratch that demonstrates method overloading for an add() method?
37+
38+
The class should include:
39+
A method add() with no parameters that adds two hardcoded numbers and prints the result.
40+
A method add(int x, int y) that returns the sum of two integers.
41+
A method add(int x, int y, int z) that prints the sum of three integers.
42+
A main method where you create an object of the class and call all three overloaded methods with sample inputs.
43+
Bonus: Explain how Java determines which overloaded method to call.
44+
*/

0 commit comments

Comments
 (0)