-
Notifications
You must be signed in to change notification settings - Fork 0
/
overloading-overriding.html
303 lines (243 loc) · 10.4 KB
/
overloading-overriding.html
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!--
~ Author: @tridib2003
~ Repository: https://github.com/tridib2003/levelupjava
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Overloading & Overriding | Level-Up Java</title>
<link href="styles.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<nav class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</nav>
<h1 class="h1-basic">Method Overloading</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
<strong>Method overloading</strong> is the process of having two or more methods within the same
class having the same name but different parameter declarations.
</p>
</li>
<li>
<p>
When an overloaded method is invoked, Java uses the type and/or number of arguments to determine
which version of the overloaded methods to execute,
which essentially means that the overloaded methods must differ in their type and/or the number of
their parameters.
</p>
</li>
<li>
<p>
The return type of overloaded methods alone is insufficient to distinguish between multiple versions
of the methods,
thus Java matches the parameters of the overloaded methods with the arguments being passed during
method invocation to resolve the ambiguity.
</p>
</li>
<li>
<p>
When methods are overloaded each version of the method can perform any activity it is designed to
do,
but as a good practice a set of overloaded methods must be designed to perform a task which has the
same underlying logic.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Demonstrate <strong>Method Overloading</strong>
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Overloading-%26-Overriding/MethodOverloadingDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<h3 class="end-section">. . .</h3>
<h1 class="h1-basic">Constructor Overloading</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
<strong>Constructor</strong> can also be overloaded in the same way as methods.
</p>
</li>
<li>
<p>
We can have multiple versions of a constructor, differing in the number of parameters.
</p>
</li>
<li>
<p>
When an object is created that version of the constructor is called whose parameters match with the
arguments being passed when it is called.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Demonstrate <strong>Constructor Overloading</strong>
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Overloading-%26-Overriding/ConstructorOverloadingDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<h3 class="end-section">. . .</h3>
<h1 class="h1-basic">Method Overriding</h1>
<div class="desc-container desc-container-center desc-std">
<hr>
<ul>
<li>
<p>
A <strong>method</strong> in a subclass is said to <strong>override</strong> a method in its
superclass, if the name and type signature of both the methods are identical.
</p>
</li>
<li>
<p>
When an overridden method is called from within its subclass, the version of the method present in
the subclass will be executed. Thus the method defined by the superclass will be hidden.
</p>
</li>
<li>
<p>
A general class defines a method common to all of its subclasses, while the subclasses inherit the
general properties from their superclass as well as add their own specialized attributes and
methods, thus achieving the <i>one interface, multiple methods</i> aspect of polymorphism. As we
move down the hierarchy from superclass to subclass we are basically moving from lesser to more
specialized classes.
</p>
</li>
<li>
<p>
<strong>Dynamic Method Dispatch</strong> is the way Java implements run time polymorphism. When a
method is overridden, which method to call is resolved during run time, rather than compile time. As
a super class reference variable can refer to a sub class object. It is the type of object being
referred to that determines which version of the overridden method will be executed.
</p>
</li>
</ul>
</div>
<br><br><br>
<div class="table-container table-container-center">
<table class="table-code-links">
<tr>
<td>
Demonstrating <strong>Method Overriding</strong>
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Overloading-%26-Overriding/MethodOverridingDemo.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Accessing overridden superclass method using <strong>super</strong>
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Overloading-%26-Overriding/MethodOverridingDemo2.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
<tr>
<td>
Demonstrating <strong>Dynamic Method Dispatch</strong>
</td>
<td>
<div class="btn-container-center">
<a class="btn-link btn-link-color1"
href="https://github.com/tridib2003/levelupjava-wiki/blob/main/Overloading-%26-Overriding/DyamicMethodDispatch.java"
target="_blank">
<i class="fa fa-github" style="font-size: 18px; color: whitesmoke;">
<p style="display: inline; margin-left: 0.4rem;">
View code
</p>
</i>
</a>
</div>
</td>
</tr>
</table>
</div>
<br><br><br>
<footer class="navigation-std">
<ul class="ul-remove-bullet nav-pills">
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Previous</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Home</a>
</li>
<li class="ul-inline-item nav-pills-decor">
<a class="link" href="/">Next</a>
</li>
</ul>
</footer>
</body>
</html>