Method Overloading

Complete Step By Step Java for Testers Inheritance, Access Modifiers And Abstraction
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€92.30
List Price:  €129.22
You save:  €36.92
£79.14
List Price:  £110.81
You save:  £31.66
CA$135.92
List Price:  CA$190.30
You save:  CA$54.37
A$153.17
List Price:  A$214.44
You save:  A$61.27
S$134.71
List Price:  S$188.59
You save:  S$53.88
HK$782.26
List Price:  HK$1,095.19
You save:  HK$312.93
CHF 90.56
List Price:  CHF 126.79
You save:  CHF 36.22
NOK kr1,076.34
List Price:  NOK kr1,506.92
You save:  NOK kr430.58
DKK kr688.47
List Price:  DKK kr963.88
You save:  DKK kr275.41
NZ$166.49
List Price:  NZ$233.09
You save:  NZ$66.60
د.إ367.19
List Price:  د.إ514.08
You save:  د.إ146.89
৳11,000.23
List Price:  ৳15,400.77
You save:  ৳4,400.53
₹8,335.05
List Price:  ₹11,669.40
You save:  ₹3,334.35
RM473.25
List Price:  RM662.57
You save:  RM189.32
₦139,656.03
List Price:  ₦195,524.03
You save:  ₦55,868
₨27,872.43
List Price:  ₨39,022.53
You save:  ₨11,150.09
฿3,637.73
List Price:  ฿5,092.97
You save:  ฿1,455.24
₺3,222.80
List Price:  ₺4,512.05
You save:  ₺1,289.25
B$497.67
List Price:  B$696.75
You save:  B$199.08
R1,888.37
List Price:  R2,643.79
You save:  R755.42
Лв180.67
List Price:  Лв252.95
You save:  Лв72.27
₩134,942
List Price:  ₩188,924.20
You save:  ₩53,982.20
₪366.06
List Price:  ₪512.51
You save:  ₪146.44
₱5,625.18
List Price:  ₱7,875.48
You save:  ₱2,250.29
¥15,118.78
List Price:  ¥21,166.90
You save:  ¥6,048.12
MX$1,663.06
List Price:  MX$2,328.35
You save:  MX$665.29
QR365.44
List Price:  QR511.63
You save:  QR146.19
P1,373.02
List Price:  P1,922.28
You save:  P549.26
KSh13,148.68
List Price:  KSh18,408.68
You save:  KSh5,260
E£4,723.53
List Price:  E£6,613.14
You save:  E£1,889.60
ብር5,694.53
List Price:  ብር7,972.57
You save:  ብር2,278.04
Kz83,612.73
List Price:  Kz117,061.17
You save:  Kz33,448.44
CLP$98,024.19
List Price:  CLP$137,237.79
You save:  CLP$39,213.60
CN¥722.49
List Price:  CN¥1,011.52
You save:  CN¥289.02
RD$5,928.03
List Price:  RD$8,299.48
You save:  RD$2,371.45
DA13,469.95
List Price:  DA18,858.47
You save:  DA5,388.52
FJ$225.25
List Price:  FJ$315.36
You save:  FJ$90.11
Q781.84
List Price:  Q1,094.62
You save:  Q312.77
GY$20,987.69
List Price:  GY$29,383.60
You save:  GY$8,395.91
ISK kr13,817.61
List Price:  ISK kr19,345.21
You save:  ISK kr5,527.60
DH1,010.57
List Price:  DH1,414.84
You save:  DH404.26
L1,763.81
List Price:  L2,469.40
You save:  L705.59
ден5,677.01
List Price:  ден7,948.05
You save:  ден2,271.03
MOP$807.66
List Price:  MOP$1,130.76
You save:  MOP$323.09
N$1,895.98
List Price:  N$2,654.45
You save:  N$758.46
C$3,689.21
List Price:  C$5,165.04
You save:  C$1,475.83
रु13,357.88
List Price:  रु18,701.57
You save:  रु5,343.68
S/371.02
List Price:  S/519.45
You save:  S/148.42
K383.60
List Price:  K537.05
You save:  K153.45
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,548.47
List Price:  ZK3,567.96
You save:  ZK1,019.49
L458.83
List Price:  L642.38
You save:  L183.55
Kč2,334.38
List Price:  Kč3,268.22
You save:  Kč933.84
Ft36,425.52
List Price:  Ft50,997.18
You save:  Ft14,571.66
SEK kr1,059.50
List Price:  SEK kr1,483.35
You save:  SEK kr423.84
ARS$85,666.43
List Price:  ARS$119,936.42
You save:  ARS$34,269.99
Bs695.09
List Price:  Bs973.16
You save:  Bs278.06
COP$389,673.99
List Price:  COP$545,559.18
You save:  COP$155,885.18
₡50,509.65
List Price:  ₡70,715.54
You save:  ₡20,205.88
L2,474.33
List Price:  L3,464.17
You save:  L989.83
₲736,638.54
List Price:  ₲1,031,323.43
You save:  ₲294,684.88
$U3,781
List Price:  $U5,293.55
You save:  $U1,512.55
zł398.17
List Price:  zł557.46
You save:  zł159.28
Already have an account? Log In

Transcript

Welcome back to the next tutorial. So in this one, we're going to talk about method overloading. So let's quickly bring up the eclipse. And let's talk about those things. So method overloading is a feature in Java that allows a class basically, this is let's say, this is a class that allows a class to have two or more methods with the same name. So till now what we have seen is we create a method in the class, and then we name it.

And then we saw that, okay, we create one name for a method, and then different names for different methods, right? But this feature allows that we can have multiple methods with the same name. The only condition is their argument list should be different. What is the argument list? This is the arguments, right? This is something we know it's arguments that's passed into a method.

So this is the name of the method, right? So what I've done is I know I have three classes. Cause overloading One, two and three right? I'm going to do all those different things in these ones. So I have three classes and all will be uploaded on the web. So let's talk about it.

Let's let's get into action. So how argument list can differ for a method, right? Let's talk about this thing. This can differ in three ways. The number one way is eliminated. So it is for you guys.

Number of parameters could be different. Right? The second way is, let's number it, data type of parameters, parameters. difference, right? And this was number one. And what's the third type?

The third type is that the sequence of data types of parameters couldn't be different. So sequence type of data type and is Right. So these are the three ways to have method overloading. This is these are the three ways to achieve method overloading. So let's, let's stop talking and let's get into action. So let me create a method.

So I'm going to take the first example. And this is cost overloading one, which is going to be the first example. And let me put what this example is. So this example is overloading type one different number of para liters. In argument, list, right? So we thought about it if the method has method could be overloaded in three different ways.

Number one, if the argument list is different, right? number of parameters could be different, right? Number two is to tell the data type of the parameters if that's different, that's number one. And number three is the sequence of data types is different, right? So that's So the first classes for the first one, right, so let's write a method. I'm going to write a method right now.

And since we're talking about cars, let's say that say something like public static, and increase seat T, right, maybe let's say C type. So I'm writing a method which should increase the height of the seat. So let's say it accepts an integer call maybe height to access or height to increase something like that. So this is a method that accepts a parameter called height to increase. So if I pass in some height, and it should increase the height of the seat, right, because we can adjust the seats anyway, right? So public static void, that's what it's complaining me.

So right here, so what I'm going to do is see so and I'm going to print something. Yeah, no increase. increase the height by whatever amount of that is passed or whatever the parameter is passed argument. So I'm just printing it out right now that I've increased the height of seed by these many inches. Right, so let's add some more text to the print statement and say inches. So that's what we are.

So this is one plain simple method right? Now how we are achieving overloading here. We mentioned that we want to achieve it by a different number of parameters in the argument list. So this is one method right? So now let's copy this and make sure we have the same name. So we have the same name, and now it's complaining.

So the method says rename method. So this is the option available rename method this one because what it's saying is duplicate method in type cars overloading man so this is my class name type cars overloading one right console loading when it says duplicate meta, because it is not accepting two methods with the same name now How we can achieve the same name. Or we can do is we can have different number of parameters, right? So the previous method had one parameter, let's say I want to keep the same method, and I want to add some more features. So in this one, what I can say is maybe add one more parameter called Boolean, and maybe, let's say, because this is this is a high end car and it can remember the setting. So let's say remember, hi.

So a chai. Tea is tea remember height? So do you guys notice as soon as I added one more parameter, the error went away, right? And now I can maybe just add one if condition just like that. This is just an example. This is not a real card implementation.

So let's say for remember, hide is true. Then print something like yeah, I, your selection, maybe something like your selection is save. Your selection is saved. So this is Something we're printing out. And if that's not working in that case, so in the else case, we can say our selection is not saved, right. So something like that we can do right is not saved.

So this is our simple plain implementation in our main method, we can just see that. So we can call both these methods with different parameters increase the height. The first one is enclosure, it accepts only integers by two, and let's call the second one, increase the height. And let's select the second one. So this one, if you guys see, this one has two. This one has two parameters, right, the first one is the one parameter in high 20s.

And the second one is high to increase. And then the second one is Boolean, remember height, so let's select this one and enter it. So let's put again two and we can pass through and since we're passing through it should go into the if condition and should print selection is saved for us, right. So that's done. And let's just quickly run the program. We ran the program here it shows for the first method, increase the height by two inches, let's pass in a different different number because it's doing Buddhist things at the same rate.

So let's pass eight inches and now running again. So the first one ran, and it says increased seat height by two inches. This is from the first method. And this is increased seat height by eight inches from second method and your selection is saved because we chose to remember height. So this is a simple simple implementation of overloading where we can have two methods with the same name, but different arguments list. Cool.

So the first way is done. Practice this guys, come back to me for questions if you have any issues, and I should be able to take care of that. So let's get back. Sorry, let's get to the second way. So I have another class created cause overloading too. And what I'm going to do is I'm just gonna at least copy stuff so I don't have to type a lot.

So put up here. So the second way is that we are talking about is. So I'm putting the comments by slash, three stars and enter. So it puts the commands, it puts the author name and gives you space to type some commands. So this one is difference in the top type of arguments. So what are the possibilities that data type of arguments that can be different, then it is possible that increase height method will have the same number of arguments.

That's possible. So let's put this let's again, copy this, and let's copy this one. But now it's again, complaining, right that duplicate methods count. So what we can do is we can change the data type of arguments. It's possible that this one accepts it and the second one can accept it. thing, and let's change it to something.

So let's type something plus, or maybe here only, I am the mpj, one, and chords and here I am II string one. So what we're doing is we again have the same name for two methods, the only difference between is the first method accepts an argument with the type in feature. And the second method accepts this argument of type string. So their data type is different. And that's good enough to overload. So what we can do is if you want to keep the same name, and they might slightly the methods might slightly different their implementation, we can implement something else inside the method.

So we can just change the data type depending on our need. And that's also possible. So when we do that, let's run this program and it's certainly possible so untangle Run it. Oh, because I'm not calling anything in the main method. So I'll have to call something. So let's say increase, hide first the integer one, so we'll pass to a pair, and the second one is string.

So let's get this chain one. And since it's a string, we'll have to pass a string format. So what we are doing in this one is the first one is just an integer. The second one accepts a string. So we pass in double quotes. So now let's run the program.

And here you go. So increase the height by two inches and the integer one, and increase height by eight inches and the string one. So that's pretty much it, guys, right? I just put up a period there. So all right, so this is the second way. Now let's get to the third.

This is the last way and this is very easy. So the last way of overloading is, let's again, put the command so you guys can easily get annoyed when you record the files. So this one is sequence. The data type of arguments, right? So what we can do is we can change the sequence of the data rights. So again, let me copy something quickly.

So I don't have to type a lot can save some time as well. So let's copy it here. And what I'm going to do is now I'm going to add one more parameter, maybe just change the name also, let's call it in variable one. And let's call it maybe, and let's add one more variable call with the type string. So string, variable, one and end wave and let's let's make this method like that. So let's say this is our method and in the system dot, print that out and then I'm going to pretend that I am the first overlord The first or no internet.

So this is done. And we are again gonna copy this, and then save this. And now you will see what I'm talking about. And I'm just gonna change it to second overload. And again, it's complaining duplicate method, again it saw that there are totally good methods. So how we can overcome is how we can overcome this is we can change the order.

So what I can do is I can put testing copied from here, because what we are trying to do is change the sequence, right? So put it after in teacher. So the only difference is in the first one string variable one came ahead, and in variable two came afterwards, and in the second one into a well two came ahead, and string variable one came afterwards. So what we did is we simply changed the order, and we were able to achieve the overloading just by this simple trick, right. So that's it. completely fair legal possible.

All done. All well said and done, let's copy these things here for Let's call these things here. So increase seat height, let's call the first one here, and the first one accepts an integer, or sorry string and then integer. So let's pass on to here, and this one is the numeric tool, and the second one is the integer one. So let's pass up a pair and then integer. So what we are doing is, in the first one, we are passing the string first and then integer.

And then in the second one, we are passing the integer first and then the string right. So you guys can see that and it's completed, done, well said and done. Now let's run this program. And it says, first, we did call the first one. So it says I'm the first alone and the second NSM the second overload. So that's pretty much it, guys.

It's pretty easy and simple, right? It looks difficult when we talk about it overloading or adding big things, but they're simple. So these are the three implementations and upload the files on the web for your reference. Go ahead guys. download them and practice a lot. That's what I always focus on.

Practice a lot. And see you in the next lectures, guys, thanks for listening. This one

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.