-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23_OverloadedMethods
More file actions
35 lines (32 loc) · 889 Bytes
/
23_OverloadedMethods
File metadata and controls
35 lines (32 loc) · 889 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// overloaded methods = methods that share the same name but have different parameters
// method name + parameters = method signature
public class Main {
public static void main(String[] args){
int x = add(1,2);
System.out.println(x);
int y = add(1,2,3);
System.out.println(y);
int z = add(1,2,3,4);
System.out.println(z);
}
static int add(int a, int b){
System.out.println("This is overloaded method #1");
return a + b;
}
static int add(int a, int b, int c){
System.out.println("This is overloaded method #2");
return a + b + c;
}
static int add(int a, int b, int c, int d){
System.out.println("This is overloaded method #3");
return a + b + c + d;
}
}
>>
This is overloaded method #1
3
This is overloaded method #2
6
This is overloaded method #3
10
- Can change int to double