forked from akash-coded/core-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodOverload.java
More file actions
154 lines (133 loc) · 4.12 KB
/
MethodOverload.java
File metadata and controls
154 lines (133 loc) · 4.12 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
class MethodOverloading {
/**
* Method-1
*/
void fun() {
System.out.println("I am a method with no parameters");
}
/**
* Method-2
*/
void fun(String param) {
System.out.println("I am a method with 1 String parameter: " + param);
}
/**
* Method-3
*/
void fun(int param) {
System.out.println("I am a method with 1 integer parameter: " + param);
}
/**
* Method-4
*/
void fun(int param1, String param2) {
System.out.printf("I am a method with 2 parameters: one integer %d and one String %s%n", param1, param2);
}
/**
* Method-5
*/
void fun(String param1, int param2) {
System.out.printf("I am also a method with 2 parameters: one string %s and one integer %d%n", param1, param2);
}
}
class NoMethodOverloadingWithReturnTypes {
// Not method overloading, but method hiding
static int add(int a, int b) {
System.out.println("Function with int return type executed");
return a + b;
}
// Unreachable
static double add(int a, int b) {
System.out.println("Function with double return type executed");
return a + b;
}
}
class MethodOverloadingWithTypePromotion {
/**
* Method-1
*/
void fun() {
System.out.println("I am a method with no parameters");
}
/**
* Method-2
*/
void fun(double param) {
System.out.println("I am a method with 1 double parameter: " + param);
}
}
class MethodOverloadingWithAmbiguousTypePromotion {
/**
* Method-1
*/
void fun() {
System.out.println("I am a method with no parameters");
}
/**
* Method-2
*/
void fun(float param) {
System.out.println("I am a method with 1 float parameter: " + param);
}
/**
* Method-3
*/
void fun(double param) {
System.out.println("I am a method with 1 double parameter: " + param);
}
}
class NoMethodOverloadingWithTypePromotion {
/**
* Method-1
*/
void fun() {
System.out.println("I am a method with no parameters");
}
/**
* Method-2
*/
void fun(int param1, double param2) {
System.out.printf("I am a method with 2 parameters: one integer %d and one double %f%n", param1, param2);
}
/**
* Method-3
*/
void fun(double param1, int param2) {
System.out.printf("I am also a method with 2 parameters: one double %f and one integer %d%n", param1, param2);
}
}
public class MethodOverload {
public static void main() {
System.out.println("User-defined main() method");
}
public static void main(String[] args) {
// MethodOverloading obj = new MethodOverloading();
// obj.fun(); // calls method-1
// obj.fun("Hello!"); // calls method-2
// obj.fun(101); // call method-3
// obj.fun(11, "Eleven"); // call method-4
// obj.fun("Nineteen", 19); // call method-5
// int sum1 = NoMethodOverloadingWithReturnTypes.add(11, 12);
// double sum2 = NoMethodOverloadingWithReturnTypes.add(11, 12);
// System.out.println("Sum stored in int variable = " + sum1);
// System.out.println("Sum stored in double variable = " + sum2);
// main(); // calls the non-parameterized main() method
// MethodOverloadingWithTypePromotion obj2 = new
// MethodOverloadingWithTypePromotion();
// obj2.fun(); // calls method-1
// obj2.fun(5.6); // calls method-2
// int num = 4;
// obj2.fun(num); // calls method-2 with type promotion
// MethodOverloadingWithAmbiguousTypePromotion obj3 = new
// MethodOverloadingWithAmbiguousTypePromotion();
// obj3.fun(); // calls method-1
// obj3.fun(5.6); // calls method-3
// obj3.fun(6.3f); // calls method-2
// obj3.fun(7);
NoMethodOverloadingWithTypePromotion obj4 = new NoMethodOverloadingWithTypePromotion();
obj4.fun(); // calls method-1
obj4.fun(2, 5.6); // calls method-2
obj4.fun(5.6, 2); // calls method-3
// obj4.fun(2, 5); // ambiuguous call
}
}