Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Method Overloading8:00 with Craig Dennis
Java allows you to have methods with the same name as long as they accept different parameters. This can be used to provide default values for methods.
So, we have a tiny issue with our code. 0:00 The only way our dispenser is going to not waste pez, 0:02 is if we eat all of them in one sitting. 0:06 I already admitted that I'm guilty of this, but 0:08 we don't wanna force feed anyone. 0:10 What if we have a half loaded dispenser, 0:13 and we wanna restock it before we say, put it in our pocket and head out. 0:15 Well currently our fill method just ignores the existing Pez count and 0:19 no matter what it sets the number of Pez to 12. 0:23 We really should have a special film method that takes a parameter of 0:26 how many pez should be loaded. 0:31 You know to hypothetically not waste these delicious treats. 0:33 Now in other languages, 0:36 this would usually mean that we did have to come up with another method name. 0:38 However, thanks to Java strict typing, 0:41 they can actually have the same name as long as their parameters are different. 0:44 Here let me show you what I mean. 0:48 So let's add a new film method that takes a specific number of pez. 0:50 So it will come in here, 0:55 and won't be very much like the other one really public when I can return anything. 0:57 It'll say fill and since the original fill method takes no parameters, but this 1:02 one will, they'll be considered different even though they have the same name. 1:07 So we wanna take in like a Pez amount here, 1:11 we want a specific amount to take in. 1:14 Oops, what did I do? 1:16 Pez amount. 1:17 Okay, so what is this gonna do? 1:21 So, why don't we just use that short hand that we just learned. 1:24 So let's say, we're going to take whatever is in pezAccount or 1:27 pezCount, and we are going to add to it pezAmount. 1:32 What using a short hand. 1:39 So now there are two fill methods, and each one can be called separately. 1:40 It just matters if you pass in the argument or not, and 1:44 the proper method will run. 1:47 If you do don't do anyone, it will run this fill. 1:49 If you do, it'll run this fill. 1:50 Cool. 1:52 Let's look in Jshell really quick. 1:53 I have it up and running. 1:55 Go ahead kick it open, and 1:55 then open up the PezDispenser.java. 1:58 So if we I think we can get to PezDispenser. 2:06 Here we go. 2:12 PezDispenser cuz it's stored in the history there. 2:13 So I'll get the dispenser out, and now I'm gonna say the pd. 2:16 and ten I'll start typing feel and do tab and 2:23 you'll see there's only one even though there's two methods. 2:25 So that's interesting because there's actually two. 2:30 So this is something that is nice about jshell what it's doing for 2:32 you is it collapsing them, but 2:36 if you do shift tab, you can see that there are actually two methods available. 2:38 Now, this overloading is a common practice, and 2:43 it's used to provide multiple entry points of how you accomplish your task. 2:47 Let's take a look at another example really quick. 2:52 So for instance the string class has quite a few overloaded methods. 2:54 So here let's check out a method that you might not have seen before. 2:57 It's called String.value in which 2:59 what you do is you can pass something and I wanna pass on a boolean, right? 3:05 And what it will do is it's gonna give me back the String.value of w which is 3:08 true read see the quotes there. 3:13 So that's a string and you can also say string.valueOf(42) just an integer, right? 3:15 And if I go in and I look at string. 3:22 So that's a static method of a string. 3:24 I just see the one, but if I do shift tab, 3:26 you'll see that all of them come back, right? 3:29 So there's the boolean that we just use there is the there's a long and 3:33 a float in a double. 3:35 But just basically wanted to show you that there are a lot of overloaded methods in 3:36 the JDK itself. 3:40 Overloading helps remove the need to come up with 3:42 other method names that do similar things. 3:45 You just use one method, but accept different arguments. 3:47 Now, because they are in fact separate methods, 3:51 you can actually use this to provide default values. 3:53 For instance, let's do that with our fill method. 3:56 So for right now we have two methods, 4:00 and they're basically doing the same thing, right? 4:01 So our original film method is just taking the maximum amount of pairs, and 4:04 adding it to what was there before, which is zero. 4:08 So why don't we call this film? 4:10 So let's get rid of this. 4:12 Let's not have it know about that. 4:13 Let's say fill with Max Pez. 4:14 So basically this method with no parameters is called it comes in and 4:18 we call this one. 4:22 And is passes this (Max_Pez) through, though it goes twelve zero plus twelve. 4:25 It's basically the same thing. 4:29 But now any time that fill is called without an amount it still works 4:31 like it used to. 4:34 It loads of the maximum number that it could, but it uses that same fill logic. 4:35 Now that has the added benefit. 4:39 It means if we make any changes to this fill method, 4:41 we don't need to make any changes here, right? 4:43 The old one gets the changes by default, and we haven't repeated ourselves. 4:46 Remember we need to keep things dry. 4:50 Don't repeat yourself. 4:52 So overloading methods is how Java provides default values to methods, and 4:54 it's just like this. 4:59 It calls the other method with a default value. 5:00 There's more in the teacher's notes. 5:02 Okay, so let's flip over to example.java. 5:04 And let's use it. 5:09 So we've got all of your pez eaten there. 5:10 Let's go ahead and let's fill it up. 5:14 So dispenser.fill and this time we're gonna fill it up before. 5:17 And then we can do it again and again so we found another pack was half empty and 5:21 we can put some in there. 5:25 And let's grab our loop again. 5:27 So again we're just gonna say 5:29 while (dispenser can dispense. 5:34 And we'll just chomp. 5:41 And here I am, repeating myself right after I said not to do that. 5:42 It's okay. We're in a file named example. 5:48 All right. 5:50 So ("Chomp!"); I'm gonna put two. 5:51 So that we know it's really big chomps. 5:54 All right. So that should be six, right? 5:58 So the first time it's gonna be zero here. 6:00 We know that. And it's gonna add 4, and 6:02 then it's gonna add another 2. 6:03 And then we're gonna loop into the chomp. 6:06 So let's go ahead and I'm gonna 6:08 I'm gonna do Ctrl+D and then do a clear. 6:12 Here we go and let's go ahead and 6:18 say clear, and javac Example.java. 6:23 And java Example. 6:29 Here we go. So that be 12 chomps and 6:31 then there should be an extra 6. 6:34 So there's the four that we loaded, and there's the two that we loaded. 6:36 Awesome, it's working. 6:39 So we just used method signatures to our advantage. 6:41 In most programming languages, 6:44 method names are what makes the method unique, but this isn't the case in Java. 6:46 The name and the parameter list are what is used to make methods unique in Java. 6:49 When there are multiple methods with the same name, 6:55 it is said to be an overloaded method. 6:58 Now, this is different than overwriting, but due to their similar sounding names, 7:00 people picking up the language get a little confused. 7:04 So again, multiple methods same name is overloading, but don't worry, 7:07 I'll remind you of this common mistake when we focus on method overwriting. 7:11 As we saw, method overloading is a great way of providing default values for 7:16 parameters. 7:21 You'll see this used quite frequently, and will also see those 7:22 overloading methods calling each other to avoid code duplication. 7:25 I'll call a few out in the standard library methods here in a few. 7:29 Did you see the critical error we just introduced? 7:33 We better get that fixed before you end up letting someone break our dispenser by 7:36 overfilling it. 7:40 [LAUGH] Hey overloading. 7:41 We don't want overloading of the Pez dispenser just in our methods, right? 7:43 There that was a really bad joke. 7:49 Okay, before we start that Pez overloading problem, let's do an exercise that make 7:51 sure that we or understand method signatures and method overload 7:55
You need to sign up for Treehouse in order to download course files.Sign up