1 00:00:00,500 --> 00:00:02,730 So, we have a tiny issue with our code. 2 00:00:02,730 --> 00:00:06,000 The only way our dispenser is going to not waste pez, 3 00:00:06,000 --> 00:00:08,530 is if we eat all of them in one sitting. 4 00:00:08,530 --> 00:00:10,650 I already admitted that I'm guilty of this, but 5 00:00:10,650 --> 00:00:12,000 we don't wanna force feed anyone. 6 00:00:13,040 --> 00:00:15,070 What if we have a half loaded dispenser, 7 00:00:15,070 --> 00:00:19,160 and we wanna restock it before we say, put it in our pocket and head out. 8 00:00:19,160 --> 00:00:23,260 Well currently our fill method just ignores the existing Pez count and 9 00:00:23,260 --> 00:00:26,590 no matter what it sets the number of Pez to 12. 10 00:00:26,590 --> 00:00:31,060 We really should have a special film method that takes a parameter of 11 00:00:31,060 --> 00:00:33,280 how many pez should be loaded. 12 00:00:33,280 --> 00:00:36,940 You know to hypothetically not waste these delicious treats. 13 00:00:36,940 --> 00:00:38,020 Now in other languages, 14 00:00:38,020 --> 00:00:41,590 this would usually mean that we did have to come up with another method name. 15 00:00:41,590 --> 00:00:44,690 However, thanks to Java strict typing, 16 00:00:44,690 --> 00:00:48,480 they can actually have the same name as long as their parameters are different. 17 00:00:48,480 --> 00:00:49,610 Here let me show you what I mean. 18 00:00:50,790 --> 00:00:55,330 So let's add a new film method that takes a specific number of pez. 19 00:00:55,330 --> 00:00:57,480 So it will come in here, 20 00:00:57,480 --> 00:01:02,390 and won't be very much like the other one really public when I can return anything. 21 00:01:02,390 --> 00:01:07,770 It'll say fill and since the original fill method takes no parameters, but this 22 00:01:07,770 --> 00:01:11,798 one will, they'll be considered different even though they have the same name. 23 00:01:11,798 --> 00:01:14,920 So we wanna take in like a Pez amount here, 24 00:01:14,920 --> 00:01:16,740 we want a specific amount to take in. 25 00:01:16,740 --> 00:01:17,830 Oops, what did I do? 26 00:01:17,830 --> 00:01:18,860 Pez amount. 27 00:01:21,340 --> 00:01:22,990 Okay, so what is this gonna do? 28 00:01:24,000 --> 00:01:27,510 So, why don't we just use that short hand that we just learned. 29 00:01:27,510 --> 00:01:31,220 So let's say, we're going to take whatever is in pezAccount or 30 00:01:32,920 --> 00:01:37,110 pezCount, and we are going to add to it pezAmount. 31 00:01:39,010 --> 00:01:40,530 What using a short hand. 32 00:01:40,530 --> 00:01:44,470 So now there are two fill methods, and each one can be called separately. 33 00:01:44,470 --> 00:01:47,640 It just matters if you pass in the argument or not, and 34 00:01:47,640 --> 00:01:49,030 the proper method will run. 35 00:01:49,030 --> 00:01:50,860 If you do don't do anyone, it will run this fill. 36 00:01:50,860 --> 00:01:52,570 If you do, it'll run this fill. 37 00:01:52,570 --> 00:01:53,780 Cool. 38 00:01:53,780 --> 00:01:55,200 Let's look in Jshell really quick. 39 00:01:55,200 --> 00:01:55,940 I have it up and running. 40 00:01:55,940 --> 00:01:58,160 Go ahead kick it open, and 41 00:01:58,160 --> 00:02:06,070 then open up the PezDispenser.java. 42 00:02:06,070 --> 00:02:12,770 So if we I think we can get to PezDispenser. 43 00:02:12,770 --> 00:02:13,960 Here we go. 44 00:02:13,960 --> 00:02:16,670 PezDispenser cuz it's stored in the history there. 45 00:02:16,670 --> 00:02:23,410 So I'll get the dispenser out, and now I'm gonna say the pd. 46 00:02:23,410 --> 00:02:25,710 and ten I'll start typing feel and do tab and 47 00:02:25,710 --> 00:02:30,110 you'll see there's only one even though there's two methods. 48 00:02:30,110 --> 00:02:32,140 So that's interesting because there's actually two. 49 00:02:32,140 --> 00:02:36,550 So this is something that is nice about jshell what it's doing for 50 00:02:36,550 --> 00:02:38,140 you is it collapsing them, but 51 00:02:38,140 --> 00:02:43,890 if you do shift tab, you can see that there are actually two methods available. 52 00:02:43,890 --> 00:02:47,340 Now, this overloading is a common practice, and 53 00:02:47,340 --> 00:02:52,420 it's used to provide multiple entry points of how you accomplish your task. 54 00:02:52,420 --> 00:02:54,060 Let's take a look at another example really quick. 55 00:02:54,060 --> 00:02:57,210 So for instance the string class has quite a few overloaded methods. 56 00:02:57,210 --> 00:02:59,838 So here let's check out a method that you might not have seen before. 57 00:02:59,838 --> 00:03:05,130 It's called String.value in which 58 00:03:05,130 --> 00:03:08,052 what you do is you can pass something and I wanna pass on a boolean, right? 59 00:03:08,052 --> 00:03:13,350 And what it will do is it's gonna give me back the String.value of w which is 60 00:03:13,350 --> 00:03:15,000 true read see the quotes there. 61 00:03:15,000 --> 00:03:21,760 So that's a string and you can also say string.valueOf(42) just an integer, right? 62 00:03:22,780 --> 00:03:24,830 And if I go in and I look at string. 63 00:03:24,830 --> 00:03:26,720 So that's a static method of a string. 64 00:03:26,720 --> 00:03:29,130 I just see the one, but if I do shift tab, 65 00:03:29,130 --> 00:03:33,050 you'll see that all of them come back, right? 66 00:03:33,050 --> 00:03:35,490 So there's the boolean that we just use there is the there's a long and 67 00:03:35,490 --> 00:03:36,910 a float in a double. 68 00:03:36,910 --> 00:03:40,610 But just basically wanted to show you that there are a lot of overloaded methods in 69 00:03:40,610 --> 00:03:42,250 the JDK itself. 70 00:03:42,250 --> 00:03:45,190 Overloading helps remove the need to come up with 71 00:03:45,190 --> 00:03:47,800 other method names that do similar things. 72 00:03:47,800 --> 00:03:51,390 You just use one method, but accept different arguments. 73 00:03:51,390 --> 00:03:53,590 Now, because they are in fact separate methods, 74 00:03:53,590 --> 00:03:56,752 you can actually use this to provide default values. 75 00:03:56,752 --> 00:04:00,080 For instance, let's do that with our fill method. 76 00:04:00,080 --> 00:04:01,560 So for right now we have two methods, 77 00:04:01,560 --> 00:04:04,250 and they're basically doing the same thing, right? 78 00:04:04,250 --> 00:04:08,490 So our original film method is just taking the maximum amount of pairs, and 79 00:04:08,490 --> 00:04:10,820 adding it to what was there before, which is zero. 80 00:04:10,820 --> 00:04:12,200 So why don't we call this film? 81 00:04:12,200 --> 00:04:13,155 So let's get rid of this. 82 00:04:13,155 --> 00:04:14,420 Let's not have it know about that. 83 00:04:14,420 --> 00:04:17,000 Let's say fill with Max Pez. 84 00:04:18,000 --> 00:04:22,900 So basically this method with no parameters is called it comes in and 85 00:04:22,900 --> 00:04:25,660 we call this one. 86 00:04:25,660 --> 00:04:29,220 And is passes this (Max_Pez) through, though it goes twelve zero plus twelve. 87 00:04:29,220 --> 00:04:31,030 It's basically the same thing. 88 00:04:31,030 --> 00:04:34,590 But now any time that fill is called without an amount it still works 89 00:04:34,590 --> 00:04:35,220 like it used to. 90 00:04:35,220 --> 00:04:39,040 It loads of the maximum number that it could, but it uses that same fill logic. 91 00:04:39,040 --> 00:04:41,020 Now that has the added benefit. 92 00:04:41,020 --> 00:04:43,430 It means if we make any changes to this fill method, 93 00:04:43,430 --> 00:04:46,630 we don't need to make any changes here, right? 94 00:04:46,630 --> 00:04:50,960 The old one gets the changes by default, and we haven't repeated ourselves. 95 00:04:50,960 --> 00:04:52,870 Remember we need to keep things dry. 96 00:04:52,870 --> 00:04:54,600 Don't repeat yourself. 97 00:04:54,600 --> 00:04:59,390 So overloading methods is how Java provides default values to methods, and 98 00:04:59,390 --> 00:05:00,570 it's just like this. 99 00:05:00,570 --> 00:05:02,960 It calls the other method with a default value. 100 00:05:02,960 --> 00:05:04,580 There's more in the teacher's notes. 101 00:05:04,580 --> 00:05:09,430 Okay, so let's flip over to example.java. 102 00:05:09,430 --> 00:05:10,790 And let's use it. 103 00:05:10,790 --> 00:05:14,580 So we've got all of your pez eaten there. 104 00:05:14,580 --> 00:05:17,010 Let's go ahead and let's fill it up. 105 00:05:17,010 --> 00:05:20,150 So dispenser.fill and this time we're gonna fill it up before. 106 00:05:21,180 --> 00:05:25,500 And then we can do it again and again so we found another pack was half empty and 107 00:05:25,500 --> 00:05:27,320 we can put some in there. 108 00:05:27,320 --> 00:05:29,570 And let's grab our loop again. 109 00:05:29,570 --> 00:05:34,958 So again we're just gonna say 110 00:05:34,958 --> 00:05:41,220 while (dispenser can dispense. 111 00:05:41,220 --> 00:05:42,530 And we'll just chomp. 112 00:05:42,530 --> 00:05:48,480 And here I am, repeating myself right after I said not to do that. 113 00:05:48,480 --> 00:05:50,614 It's okay. We're in a file named example. 114 00:05:50,614 --> 00:05:51,404 All right. 115 00:05:51,404 --> 00:05:54,350 So ("Chomp!"); I'm gonna put two. 116 00:05:54,350 --> 00:05:56,640 So that we know it's really big chomps. 117 00:05:58,380 --> 00:06:00,190 All right. So that should be six, right? 118 00:06:00,190 --> 00:06:02,210 So the first time it's gonna be zero here. 119 00:06:02,210 --> 00:06:03,840 We know that. And it's gonna add 4, and 120 00:06:03,840 --> 00:06:05,225 then it's gonna add another 2. 121 00:06:06,250 --> 00:06:08,010 And then we're gonna loop into the chomp. 122 00:06:08,010 --> 00:06:09,905 So let's go ahead and I'm gonna 123 00:06:12,928 --> 00:06:17,230 I'm gonna do Ctrl+D and then do a clear. 124 00:06:18,600 --> 00:06:23,452 Here we go and let's go ahead and 125 00:06:23,452 --> 00:06:29,397 say clear, and javac Example.java. 126 00:06:29,397 --> 00:06:31,877 And java Example. 127 00:06:31,877 --> 00:06:34,081 Here we go. So that be 12 chomps and 128 00:06:34,081 --> 00:06:36,970 then there should be an extra 6. 129 00:06:36,970 --> 00:06:39,610 So there's the four that we loaded, and there's the two that we loaded. 130 00:06:39,610 --> 00:06:41,660 Awesome, it's working. 131 00:06:41,660 --> 00:06:44,650 So we just used method signatures to our advantage. 132 00:06:44,650 --> 00:06:46,170 In most programming languages, 133 00:06:46,170 --> 00:06:49,720 method names are what makes the method unique, but this isn't the case in Java. 134 00:06:49,720 --> 00:06:54,460 The name and the parameter list are what is used to make methods unique in Java. 135 00:06:55,750 --> 00:06:58,140 When there are multiple methods with the same name, 136 00:06:58,140 --> 00:07:00,600 it is said to be an overloaded method. 137 00:07:00,600 --> 00:07:04,650 Now, this is different than overwriting, but due to their similar sounding names, 138 00:07:04,650 --> 00:07:07,430 people picking up the language get a little confused. 139 00:07:07,430 --> 00:07:11,920 So again, multiple methods same name is overloading, but don't worry, 140 00:07:11,920 --> 00:07:15,310 I'll remind you of this common mistake when we focus on method overwriting. 141 00:07:16,580 --> 00:07:21,170 As we saw, method overloading is a great way of providing default values for 142 00:07:21,170 --> 00:07:22,150 parameters. 143 00:07:22,150 --> 00:07:25,460 You'll see this used quite frequently, and will also see those 144 00:07:25,460 --> 00:07:29,300 overloading methods calling each other to avoid code duplication. 145 00:07:29,300 --> 00:07:32,180 I'll call a few out in the standard library methods here in a few. 146 00:07:33,460 --> 00:07:36,500 Did you see the critical error we just introduced? 147 00:07:36,500 --> 00:07:40,319 We better get that fixed before you end up letting someone break our dispenser by 148 00:07:40,319 --> 00:07:41,176 overfilling it. 149 00:07:41,176 --> 00:07:43,480 [LAUGH] Hey overloading. 150 00:07:43,480 --> 00:07:49,210 We don't want overloading of the Pez dispenser just in our methods, right? 151 00:07:49,210 --> 00:07:51,510 There that was a really bad joke. 152 00:07:51,510 --> 00:07:55,550 Okay, before we start that Pez overloading problem, let's do an exercise that make 153 00:07:55,550 --> 00:07:59,700 sure that we or understand method signatures and method overload