-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathMethodOvrloading.java
78 lines (58 loc) · 1.95 KB
/
MethodOvrloading.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
class MethodOverload {
// Method Overloading via fun()
void fun() { // Function 1
int x = 5;
System.out.println("I am a function with no args");
}
void fun(String arg) { // Function 2
System.out.println("I am a function with args: " + arg);
}
void fun(int arg) { // Function 3
System.out.println("I am a function with args: " + arg);
}
void fun(int arg1, String arg2) { // Function 4
System.out.println("I am a function with args: " + arg1 + " " + arg2);
}
void fun(String arg1, int arg2) { // Function 5
System.out.println("I am a function with args: " + arg1 + " " + arg2);
}
void fun(int x, double... y) { // Function 6
System.out.print("I am an overloaded function with varargs... " + x);
for (double d : y) {
System.out.print(" " + d);
}
}
// void fun(int arg1, double arg2) { // Function 7
// System.out.println("I am a function with args: " + arg1 + " " + arg2);
// }
// public void fun(String arg) { // Function 2
// System.out.println("I am a function with args: " + arg);
// }
// int fun(int arg) { // Function 6
// System.out.println("I am a function with args: " + arg);
// return 100;
// }
}
class MethodOverloadWentWrong {
void fun(int x, double y) {
System.out.println("Int first, Double second" + x + " " + y);
}
void fun(double x, int y) {
System.out.println("Double first, Int second" + x + " " + y);
}
}
public class MethodOvrloading {
public static void main(String[] args) {
MethodOverload obj = new MethodOverload();
obj.fun();
obj.fun("Wiley");
obj.fun(20);
obj.fun(20, "Wiley");
obj.fun("Wiley", 20);
byte b = 20, c = 30;
obj.fun(b);
obj.fun(b, 20.0, 30);
// MethodOverloadWentWrong obj = new MethodOverloadWentWrong();
// obj.fun(20, 20.0);
}
}