1
00:00:00,474 --> 00:00:03,057
(upbeat music)

2
00:00:05,374 --> 00:00:06,980
When we started looking at classes,

3
00:00:06,980 --> 00:00:09,014
I mentioned that there's less need to overload

4
00:00:09,014 --> 00:00:10,720
functions in Kotlin.

5
00:00:10,720 --> 00:00:12,900
And that's because Kotlin allows default values

6
00:00:12,900 --> 00:00:15,936
to be specified for function arguments.

7
00:00:15,936 --> 00:00:19,020
Now, I didn't say very much about overloading at the time,

8
00:00:19,020 --> 00:00:22,097
so let's look at what that is in a bit more detail.

9
00:00:22,097 --> 00:00:24,355
Now, when you overload a function,

10
00:00:24,355 --> 00:00:26,707
that just means creating another copy of it

11
00:00:26,707 --> 00:00:28,546
with different parameters.

12
00:00:28,546 --> 00:00:29,910
So that could be very useful here

13
00:00:29,910 --> 00:00:32,835
because we could use a different version of drop loot

14
00:00:32,835 --> 00:00:35,225
and give it the name of the loot.

15
00:00:35,225 --> 00:00:36,747
So, we could come down here,

16
00:00:36,747 --> 00:00:39,491
at the moment we've got on line 51,

17
00:00:39,491 --> 00:00:41,372
the definition for blue potion,

18
00:00:41,372 --> 00:00:45,027
but in terms of drop loot, we could do something like,

19
00:00:45,027 --> 00:00:46,027
tim.droploot

20
00:00:47,739 --> 00:00:48,572
and actually enter the name.

21
00:00:48,572 --> 00:00:50,572
So, invisibility potion.

22
00:00:52,453 --> 00:00:53,840
Now, we've got an error at the moment

23
00:00:53,840 --> 00:00:56,986
because we're passing a string to the drop loot function

24
00:00:56,986 --> 00:00:59,410
and it expects a loot object.

25
00:00:59,410 --> 00:01:01,870
Now, if we could pass a string to drop loot,

26
00:01:01,870 --> 00:01:04,602
then we could drop the invisibility potion,

27
00:01:04,602 --> 00:01:07,047
even though we don't have a reference to it.

28
00:01:07,047 --> 00:01:08,573
So what we need to do here,

29
00:01:08,573 --> 00:01:10,479
is create a version of drop loot

30
00:01:10,479 --> 00:01:12,178
that accepts the string argument,

31
00:01:12,178 --> 00:01:15,939
then get it to remove the corresponding item from the list.

32
00:01:15,939 --> 00:01:17,719
So let's go to our player class,

33
00:01:17,719 --> 00:01:22,352
we're gonna add this after the existing drop loot method.

34
00:01:22,352 --> 00:01:26,417
Alright, so we go to start, and drop loot,

35
00:01:26,417 --> 00:01:27,334
parentheses

36
00:01:28,767 --> 00:01:29,600
name,

37
00:01:30,646 --> 00:01:31,729
colon string,

38
00:01:33,354 --> 00:01:34,604
colon, boolean.

39
00:01:36,507 --> 00:01:37,615
Then we create the function with

40
00:01:37,615 --> 00:01:39,518
the left and right curly braces.

41
00:01:39,518 --> 00:01:41,685
And we'll just do println,

42
00:01:43,242 --> 00:01:44,830
$name

43
00:01:44,830 --> 00:01:46,163
will be dropped.

44
00:01:49,071 --> 00:01:52,202
For now, we're just gonna do return

45
00:01:52,202 --> 00:01:53,145
true.

46
00:01:53,145 --> 00:01:54,700
So when overloading a function,

47
00:01:54,700 --> 00:01:57,581
we create another function with the same name

48
00:01:57,581 --> 00:01:59,504
but we give it different parameters.

49
00:01:59,504 --> 00:02:01,567
Now, the compiler checks the parameters,

50
00:02:01,567 --> 00:02:04,425
and calls the version of the overloaded function

51
00:02:04,425 --> 00:02:06,464
that has matching parameters.

52
00:02:06,464 --> 00:02:08,342
So, we've got two versions of drop loot now,

53
00:02:08,342 --> 00:02:09,573
as you can see.

54
00:02:09,573 --> 00:02:11,963
So this first one, on line 32,

55
00:02:11,963 --> 00:02:14,148
takes an argument of top loot

56
00:02:14,148 --> 00:02:16,796
because we've defined the parameter to be loot.

57
00:02:16,796 --> 00:02:19,090
Now, the second drop loot, on line 41,

58
00:02:19,090 --> 00:02:20,694
takes a string argument.

59
00:02:20,694 --> 00:02:22,380
Now, it doesn't do anything useful yet,

60
00:02:22,380 --> 00:02:25,183
we're focusing on the overloading mechanism at the moment.

61
00:02:25,183 --> 00:02:26,994
We'll actually add more code to the second version

62
00:02:26,994 --> 00:02:27,827
in a minute.

63
00:02:27,827 --> 00:02:29,668
So go back to main now.

64
00:02:29,668 --> 00:02:31,751
Notice that the error disappeared.

65
00:02:31,751 --> 00:02:35,882
And the first time we call our drop loot,

66
00:02:35,882 --> 00:02:38,427
this code here on line 45,

67
00:02:38,427 --> 00:02:39,432
actually this is probably a better one here,

68
00:02:39,432 --> 00:02:41,998
so let's do this one on line 52.

69
00:02:41,998 --> 00:02:43,584
So, we're calling drop loot there and we're

70
00:02:43,584 --> 00:02:46,417
and we're giving it a loot instance, blue potion,

71
00:02:46,417 --> 00:02:49,521
and by doing that, the first drop loot function's called.

72
00:02:49,521 --> 00:02:51,686
But down here, when we call it with a string argument,

73
00:02:51,686 --> 00:02:54,206
invisibility potion, in line 58,

74
00:02:54,206 --> 00:02:56,362
then that's the second drop loot function

75
00:02:56,362 --> 00:02:58,710
that's gonna be called instead,

76
00:02:58,710 --> 00:03:01,681
because we're past the argument of top string.

77
00:03:01,681 --> 00:03:06,348
So let's actually try running this and see what happens.

78
00:03:09,279 --> 00:03:11,071
And you can see that the other codes executed,

79
00:03:11,071 --> 00:03:12,050
then right down the bottom,

80
00:03:12,050 --> 00:03:14,730
invisibility potion will be dropped.

81
00:03:14,730 --> 00:03:17,285
So that's working in our second version of drop loots,

82
00:03:17,285 --> 00:03:20,304
obviously being called at that point.

83
00:03:20,304 --> 00:03:22,103
So that's function overloading,

84
00:03:22,103 --> 00:03:24,272
you create another version of the function

85
00:03:24,272 --> 00:03:25,725
with different parameters

86
00:03:25,725 --> 00:03:29,231
and Kotlin calls the one that matches the argument types.

87
00:03:29,231 --> 00:03:32,110
Now, there's less need to overload functions in Kotlin

88
00:03:32,110 --> 00:03:33,405
than there is in Java,

89
00:03:33,405 --> 00:03:35,248
because again we can use default values

90
00:03:35,248 --> 00:03:36,965
for the parameters instead.

91
00:03:36,965 --> 00:03:39,929
But it's still necessary to overload functions sometimes

92
00:03:39,929 --> 00:03:41,975
and this is one example.

93
00:03:41,975 --> 00:03:43,372
Alright so we're gonna finish this video up

94
00:03:43,372 --> 00:03:46,557
by making our overloaded drop loot function work.

95
00:03:46,557 --> 00:03:49,598
Now, the Kotlin collection classes have a removed

96
00:03:49,598 --> 00:03:50,525
IF function,

97
00:03:50,525 --> 00:03:52,689
that we can use to remove an item from the list

98
00:03:52,689 --> 00:03:54,849
if a certain condition's true.

99
00:03:54,849 --> 00:03:56,211
So let's actually try that.

100
00:03:56,211 --> 00:03:57,811
We'll go back to player,

101
00:03:57,811 --> 00:03:58,702
then player class,

102
00:03:58,702 --> 00:04:00,482
and our second drop loot function,

103
00:04:00,482 --> 00:04:03,145
we're gonna do return, we're gonna delete true

104
00:04:03,145 --> 00:04:06,317
and instead we're gonna return

105
00:04:06,317 --> 00:04:07,817
inventory.removeIf

106
00:04:10,980 --> 00:04:12,907
and we're gonna delete the parentheses

107
00:04:12,907 --> 00:04:14,656
and we're gonna add a code block instead.

108
00:04:14,656 --> 00:04:16,059
And within there we're gonna put

109
00:04:16,059 --> 00:04:18,142
it.name == name

110
00:04:20,702 --> 00:04:22,036
Two equals signs there.

111
00:04:22,036 --> 00:04:23,898
So that checks each item in the list

112
00:04:23,898 --> 00:04:25,901
and removes it if the name is the same

113
00:04:25,901 --> 00:04:28,365
as the name we passed into drop loot.

114
00:04:28,365 --> 00:04:30,806
Now, it here, where the it.

115
00:04:30,806 --> 00:04:32,507
well, that refers to the item

116
00:04:32,507 --> 00:04:34,240
that's currently being checked.

117
00:04:34,240 --> 00:04:35,667
So if the name of the current item

118
00:04:35,667 --> 00:04:38,331
matches invisibility potion in our example,

119
00:04:38,331 --> 00:04:41,233
then the item gets removed from the list.

120
00:04:41,233 --> 00:04:44,375
Remove if then moves on to the next item on the list

121
00:04:44,375 --> 00:04:45,518
and repeats the check.

122
00:04:45,518 --> 00:04:48,657
Now, if any items are removed, it returns true,

123
00:04:48,657 --> 00:04:50,408
otherwise it returns false.

124
00:04:50,408 --> 00:04:52,363
Which is what we want the function to do,

125
00:04:52,363 --> 00:04:55,559
so we can return whatever remove if returns.

126
00:04:55,559 --> 00:04:57,168
So let's see it working.

127
00:04:57,168 --> 00:04:59,457
What I'm gonna do is print out Tim's inventory

128
00:04:59,457 --> 00:05:00,428
at the end of main,

129
00:05:00,428 --> 00:05:02,339
so we can check that the invisibility potion

130
00:05:02,339 --> 00:05:03,683
has been removed.

131
00:05:03,683 --> 00:05:05,579
So we'll go back to our main,

132
00:05:05,579 --> 00:05:08,001
making sure that I've actually spelled it the same here,

133
00:05:08,001 --> 00:05:09,682
it's being added as invisibility potion

134
00:05:09,682 --> 00:05:12,181
and what is being removed is invisibility potion.

135
00:05:12,181 --> 00:05:14,748
And I'll type tim.showInventory

136
00:05:14,748 --> 00:05:18,165
and let's see how that goes if we run it.

137
00:05:20,754 --> 00:05:23,278
And you can see Tim's inventory down there and here

138
00:05:23,278 --> 00:05:24,266
and invisibility potion,

139
00:05:24,266 --> 00:05:25,811
which was there on the previous call,

140
00:05:25,811 --> 00:05:27,557
has now been removed.

141
00:05:27,557 --> 00:05:28,656
And we've only got the chest armour

142
00:05:28,656 --> 00:05:30,340
and the ring of protection left.

143
00:05:30,340 --> 00:05:31,745
So that's working fine.

144
00:05:31,745 --> 00:05:33,444
Now, just in case you're wondering,

145
00:05:33,444 --> 00:05:35,360
we don't have to use the return values

146
00:05:35,360 --> 00:05:36,789
when calling a function.

147
00:05:36,789 --> 00:05:38,805
So, we could have done the same thing as we did before,

148
00:05:38,805 --> 00:05:40,280
for the blue potion.

149
00:05:40,280 --> 00:05:42,262
So we could have done something like this:

150
00:05:42,262 --> 00:05:43,095
if

151
00:05:44,355 --> 00:05:47,738
and surround that in parentheses,

152
00:05:47,738 --> 00:05:51,478
add a code block and then put tim.showInventory,

153
00:05:51,478 --> 00:05:53,395
and else, your println,

154
00:05:59,206 --> 00:06:02,539
"you don't have an invisibility potion."

155
00:06:05,524 --> 00:06:06,503
So if you do that instead,

156
00:06:06,503 --> 00:06:09,164
and I'll delete that call to show inventory method.

157
00:06:09,164 --> 00:06:10,664
So let's run that.

158
00:06:15,070 --> 00:06:17,569
And you can see we get the same result in this case,

159
00:06:17,569 --> 00:06:19,354
with only the two items left.

160
00:06:19,354 --> 00:06:21,717
Alright, so I think we got to the end of this video.

161
00:06:21,717 --> 00:06:24,232
Now, that's probably quite a bit to digest,

162
00:06:24,232 --> 00:06:26,949
so go back and watch the last few videos a few more times

163
00:06:26,949 --> 00:06:28,004
if you need to,

164
00:06:28,004 --> 00:06:29,896
and go through step by step.

165
00:06:29,896 --> 00:06:31,107
Now, it'll make more sense

166
00:06:31,107 --> 00:06:33,063
the more times that you go through it.

167
00:06:33,063 --> 00:06:35,556
And if you watch this and you aren't following along,

168
00:06:35,556 --> 00:06:37,467
I do suggest that you go back and actually type this in

169
00:06:37,467 --> 00:06:38,668
as I'm typing it in.

170
00:06:38,668 --> 00:06:40,676
What I found when I was learning to programme

171
00:06:40,676 --> 00:06:42,138
was that typing the code in,

172
00:06:42,138 --> 00:06:43,695
rather than just reading it,

173
00:06:43,695 --> 00:06:45,931
really helped to consolidate things.

174
00:06:45,931 --> 00:06:46,764
Back then of course,

175
00:06:46,764 --> 00:06:49,252
there wasn't really much in the way of video tutorials.

176
00:06:49,252 --> 00:06:51,557
What we did have was a whole load of computers released

177
00:06:51,557 --> 00:06:53,145
at around the same time.

178
00:06:53,145 --> 00:06:56,229
Such as Sinclair ZX 81, Commodore PET,

179
00:06:56,229 --> 00:06:57,993
BBC computer made by Acorn,

180
00:06:57,993 --> 00:06:59,504
and a whole lot of other machines.

181
00:06:59,504 --> 00:07:01,084
And there was also monthly magazines

182
00:07:01,084 --> 00:07:03,348
and we learned a lot from reading the manuals

183
00:07:03,348 --> 00:07:04,849
but also from typing in the code

184
00:07:04,849 --> 00:07:07,037
that was published in those magazines.

185
00:07:07,037 --> 00:07:09,106
Now obviously you'll make errors when typing the code in

186
00:07:09,106 --> 00:07:10,673
and debugging the programmes to find the errors

187
00:07:10,673 --> 00:07:13,369
and make them work, was also a great way to learn.

188
00:07:13,369 --> 00:07:16,296
So, typing it in seems to solidify the information

189
00:07:16,296 --> 00:07:18,881
and make it a lot easier to understand.

190
00:07:18,881 --> 00:07:20,300
So I certainly recommend that you don't just

191
00:07:20,300 --> 00:07:21,741
watch these videos,

192
00:07:21,741 --> 00:07:23,554
but you also act and be typing this in

193
00:07:23,554 --> 00:07:25,791
as the video progresses.

194
00:07:25,791 --> 00:07:28,916
And it addition, also try modifying the code

195
00:07:28,916 --> 00:07:30,088
to try different things.

196
00:07:30,088 --> 00:07:31,883
Start off simple things, such as,

197
00:07:31,883 --> 00:07:34,344
perhaps adding an extra field to one of the classes,

198
00:07:34,344 --> 00:07:36,477
then by making other improvements.

199
00:07:36,477 --> 00:07:38,125
You'll almost certainly get errors

200
00:07:38,125 --> 00:07:40,147
but sorting out the errors is a great way

201
00:07:40,147 --> 00:07:43,444
to increase your understanding of how it all works.

202
00:07:43,444 --> 00:07:44,470
You can't do any damage.

203
00:07:44,470 --> 00:07:46,523
The worse that will happen is you get an error message.

204
00:07:46,523 --> 00:07:48,086
Now that wasn't always the case, by the way.

205
00:07:48,086 --> 00:07:50,385
It was possible to speed up the screen

206
00:07:50,385 --> 00:07:52,716
on an early computer called the Commodore PET,

207
00:07:52,716 --> 00:07:55,537
by writing certain values to specific memory locations,

208
00:07:55,537 --> 00:07:57,052
and if you got that wrong,

209
00:07:57,052 --> 00:07:59,133
the display would be permanently damaged.

210
00:07:59,133 --> 00:08:00,218
But that's no longer the case

211
00:08:00,218 --> 00:08:02,188
and you literally can't do that these days

212
00:08:02,188 --> 00:08:04,021
with modern computers.

213
00:08:04,021 --> 00:08:05,734
Basically you can't do any damage to your computer

214
00:08:05,734 --> 00:08:08,796
with Kotlin, so have fun and try things out.

215
00:08:08,796 --> 00:08:10,343
Alright, so I've hoped you enjoyed this

216
00:08:10,343 --> 00:08:13,252
and I will see you in the next video.

