-
Notifications
You must be signed in to change notification settings - Fork 13
/
MethodOverloading.java
99 lines (82 loc) · 3.22 KB
/
MethodOverloading.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package lessons.methods.methodoverloading;
public class MethodOverloading {
public static void main(String[] args) {
// // Sample Method for see to explanation method and candidates
// System.out.println("Efficient House");
//
// // When methods 1, 2 are written
// lessons.methods.methodoverloading.MyMethods.foo(5, 5);
//
// // lessons.methods.methodoverloading.MethodOverloading.foo(5,5); -> reaching with source path
// MyMethods.foo(5, 5);
// MyMethods.foo(5, 5.0);
//
// // When YourMethod class was written
//
// // YourMethods yourMethod = new YourMethods();
// // yourMethod.foo(5,5); -> There is no OVERLOADING
//
// // When methods 1, 2, 3, 4 are written
// MyMethods myMethod = new MyMethods();
// myMethod.foo(5.5, 5);
// myMethod.foo(5, 5);
// When just 7, 8, 9 methods are written (AMBIGUITY)
short s = 5;
MyMethods.foo(s, 5);
}
}
class MyMethods {
// 1
public static void foo(int a, int b) {
System.out.println("You are in 1. method -> void method(int, int)");
System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b);
}
// 2
public static void foo(int a, double b) {
System.out.println("You are in 2. method -> void method(int, double)");
System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b);
}
// 3
public static void foo(short a, long b) {
System.out.println("You are in 3. method -> void method(short, long)");
System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b);
}
// 4 non-static
public void foo(double a, int b) {
System.out.println("You are in 4. method -> void method(double, int)");
System.out.printf("You called this method -> MyMethod.method(%.2f, %d);", a, b);
}
// 5
public static void foo(short a, byte b) {
System.out.println("You are in 5. method -> void method(short, byte)");
System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b);
}
// 6
public static void foo(short a, int b) {
System.out.println("You are in 6. method -> void method(short, int)");
System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b);
}
// 7
public static void foo(short a, double b) {
System.out.println("You are in 7. method -> void method(short, double)");
System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b);
}
// 8
public static void foo(int a, float b) {
System.out.println("You are in 8. method -> void method(int, float)");
System.out.printf("You called this method -> MyMethod.method(%d, %.2f);", a, b);
}
// 9
public static void foo(int a, long b) {
System.out.println("You are in 9. method -> void method(int, long)");
System.out.printf("You called this method -> MyMethod.method(%d, %d);", a, b);
}
}
class YourMethods {
public void foo(int a, int b) {
System.out.println("This is YourMethods class");
}
// public static void foo(int a, int b) {
// System.out.println("This is YourMethods class");
// }
}