1
00:00:00,212 --> 00:00:02,795
(upbeat music)

2
00:00:05,600 --> 00:00:06,700
In this video we're gonna look at

3
00:00:06,700 --> 00:00:10,470
extending the Vampyre class to create a super Vampyre.

4
00:00:10,470 --> 00:00:11,960
Now, it's going to be very similar

5
00:00:11,960 --> 00:00:13,450
to what we've already done,

6
00:00:13,450 --> 00:00:14,570
but there's a couple of other things

7
00:00:14,570 --> 00:00:16,420
we need to know how to do.

8
00:00:16,420 --> 00:00:18,960
So we're gonna create a vampyre king subclass

9
00:00:18,960 --> 00:00:20,610
of Vampyre.

10
00:00:20,610 --> 00:00:23,370
Now, our vampyre king is going to be incredibly powerful,

11
00:00:23,370 --> 00:00:25,670
and any points of damage inflicted will be

12
00:00:25,670 --> 00:00:27,260
divided by four.

13
00:00:27,260 --> 00:00:28,980
Vampyre king objects will also start off

14
00:00:28,980 --> 00:00:31,970
with 140 hit points and as you see,

15
00:00:31,970 --> 00:00:33,440
that's where we need to look a bit deeper

16
00:00:33,440 --> 00:00:35,750
into Kotlin inheritance.

17
00:00:35,750 --> 00:00:37,720
So we're gonna start by extending Vampyre

18
00:00:37,720 --> 00:00:39,340
to create a vampyre king class

19
00:00:39,340 --> 00:00:41,620
with those additional properties.

20
00:00:41,620 --> 00:00:44,080
First thing we need to do is start by making

21
00:00:44,080 --> 00:00:45,433
the Vampyre class open.

22
00:00:46,320 --> 00:00:47,233
So let's do that.

23
00:00:48,680 --> 00:00:50,850
So that it can now be inherited from.

24
00:00:50,850 --> 00:00:53,300
And now we need to create our new vampyre king class.

25
00:00:53,300 --> 00:00:54,270
I'm going to do that.

26
00:00:54,270 --> 00:00:57,883
Right click Java folder, New, Kotlin Class

27
00:00:57,883 --> 00:01:00,970
save a bit of typing by selecting Class

28
00:01:00,970 --> 00:01:03,003
and the name of your VampyreKing.

29
00:01:06,930 --> 00:01:08,800
Now we need to pass the name to the class

30
00:01:08,800 --> 00:01:11,070
and provide that to the Vampyre class.

31
00:01:11,070 --> 00:01:13,290
So, the end of the definition

32
00:01:13,290 --> 00:01:17,200
open parentheses, name colon String,

33
00:01:17,200 --> 00:01:19,240
then outside the right parentheses,

34
00:01:19,240 --> 00:01:21,523
colon Vampyre,

35
00:01:22,570 --> 00:01:25,700
then parentheses, name.

36
00:01:25,700 --> 00:01:28,600
That's straightforward, but where it gets tricky

37
00:01:28,600 --> 00:01:32,343
is setting the VampyreKing's hit points to 140.

38
00:01:33,290 --> 00:01:35,670
Now if we look at the Vampyre class,

39
00:01:35,670 --> 00:01:39,980
it passes the fixed value 20 to the enemy constructor.

40
00:01:39,980 --> 00:01:42,910
Now we need to change that to 140 somehow.

41
00:01:42,910 --> 00:01:46,500
Now, one way we could do that is to use a default value,

42
00:01:46,500 --> 00:01:48,210
and we can modify the Vampyre class

43
00:01:48,210 --> 00:01:50,140
to have a hit points parameter,

44
00:01:50,140 --> 00:01:51,703
with a default value of 20,

45
00:01:51,703 --> 00:01:54,210
that we can then pass to the enemy constructor.

46
00:01:54,210 --> 00:01:56,310
So let's try that first.

47
00:01:56,310 --> 00:01:59,760
So we'll add the parameter, hitPoints colon Int

48
00:02:03,570 --> 00:02:05,203
and set the default value of 20.

49
00:02:06,080 --> 00:02:06,913
Then we need to change

50
00:02:06,913 --> 00:02:10,267
the hardcoded value here to hitPoints.

51
00:02:12,000 --> 00:02:13,170
So now that we've done that,

52
00:02:13,170 --> 00:02:15,700
we can go back to the Vampyre constructor

53
00:02:15,700 --> 00:02:17,403
in the VampyreKing class,

54
00:02:19,430 --> 00:02:21,733
and we can change that to 140.

55
00:02:23,970 --> 00:02:25,670
So let's see if that works

56
00:02:25,670 --> 00:02:27,860
before we talk some more about it.

57
00:02:27,860 --> 00:02:30,220
So we go back to our main function,

58
00:02:30,220 --> 00:02:34,170
and let's add or create a VampyreKing instance.

59
00:02:34,170 --> 00:02:39,150
So val dracula is equal to

60
00:02:40,558 --> 00:02:42,863
VampyreKing parentheses Dracula.

61
00:02:44,670 --> 00:02:45,910
And let's print the value of that as well,

62
00:02:45,910 --> 00:02:46,947
so println(dracula).

63
00:02:49,446 --> 00:02:51,346
So let's run that and see if it works.

64
00:02:55,360 --> 00:02:58,340
Okay so that's good, we can see that it does work.

65
00:02:58,340 --> 00:02:59,850
Dracula, as you can see down at the bottom here,

66
00:02:59,850 --> 00:03:02,020
has got 140 hit points.

67
00:03:02,020 --> 00:03:03,370
So that's one way to do it,

68
00:03:03,370 --> 00:03:05,820
and it's a perfectly-acceptable solution,

69
00:03:05,820 --> 00:03:07,780
but you can probably tell that I'm itching to show you

70
00:03:07,780 --> 00:03:08,820
another way.

71
00:03:08,820 --> 00:03:11,150
So, what's wrong with what we've just done?

72
00:03:11,150 --> 00:03:14,920
Well, it's compromised our Vampyre class slightly,

73
00:03:14,920 --> 00:03:17,270
because we can now create Vampyre instances

74
00:03:17,270 --> 00:03:19,330
with less than 20 hit points.

75
00:03:19,330 --> 00:03:21,930
So if you go and modify the code on line eight,

76
00:03:21,930 --> 00:03:23,710
that creates Vlad,

77
00:03:23,710 --> 00:03:25,910
we can use that second argument now, and say,

78
00:03:25,910 --> 00:03:27,910
set the hit points to two.

79
00:03:27,910 --> 00:03:29,330
Now, before I run the programme again,

80
00:03:29,330 --> 00:03:30,690
notice in the output

81
00:03:30,690 --> 00:03:32,660
that Vlad started off with 20 hit points,

82
00:03:32,660 --> 00:03:36,130
and that dropped to 16 after we inflicted the damage.

83
00:03:36,130 --> 00:03:37,480
But if we run it again now,

84
00:03:40,610 --> 00:03:41,680
what's happened this time is

85
00:03:41,680 --> 00:03:43,820
because he only started out with two hit points,

86
00:03:43,820 --> 00:03:45,800
poor old Vlad's lost a life

87
00:03:45,800 --> 00:03:48,740
with the damage that's been inflicted on him.

88
00:03:48,740 --> 00:03:51,350
Now the change we made to the Vampyre class

89
00:03:51,350 --> 00:03:53,800
now allows vampyres to be created with a different number

90
00:03:53,800 --> 00:03:54,930
of hit points.

91
00:03:54,930 --> 00:03:57,870
And that may be fine, depending on your application.

92
00:03:57,870 --> 00:03:59,930
Sometimes, though, you'll want to provide a default

93
00:03:59,930 --> 00:04:02,700
but allow the users of your class to change it.

94
00:04:02,700 --> 00:04:04,750
Sometimes, though, it'll be very important

95
00:04:04,750 --> 00:04:07,779
that some properties can't be altered in this way.

96
00:04:07,779 --> 00:04:10,430
Now, as an example, consider an arc class,

97
00:04:10,430 --> 00:04:14,140
that draws an arc, through a specified number of degrees.

98
00:04:14,140 --> 00:04:17,339
Now, we may override this to create a circle class,

99
00:04:17,339 --> 00:04:20,410
which will have the number of degrees fixed at 360.

100
00:04:20,410 --> 00:04:22,029
Now we certainly don't want to draw circles

101
00:04:22,029 --> 00:04:24,500
that don't have 360 degrees, because of course,

102
00:04:24,500 --> 00:04:26,020
they wouldn't be circles.

103
00:04:26,020 --> 00:04:28,010
So, getting back to our code though,

104
00:04:28,010 --> 00:04:30,920
we can define our enemy objects to be whatever we want.

105
00:04:30,920 --> 00:04:32,550
There are inventions if you'd like,

106
00:04:32,550 --> 00:04:35,180
but when we're modelling real physical objects,

107
00:04:35,180 --> 00:04:37,090
we want our classes to really respect

108
00:04:37,090 --> 00:04:38,740
those physical constraints.

109
00:04:38,740 --> 00:04:41,997
And that's why sometimes this approach won't be appropriate.

110
00:04:41,997 --> 00:04:44,110
And another important reason is that

111
00:04:44,110 --> 00:04:46,680
we might be extending someone else's class,

112
00:04:46,680 --> 00:04:49,010
and if we don't have the source code available,

113
00:04:49,010 --> 00:04:50,870
then we can't make changes to it,

114
00:04:50,870 --> 00:04:53,240
and that would rule out this approach.

115
00:04:53,240 --> 00:04:55,740
So what I'm gonna do is undo these changes,

116
00:04:55,740 --> 00:04:57,800
and Command + Z is a very useful shortcut.

117
00:04:57,800 --> 00:05:00,140
That undoes the last change we made.

118
00:05:00,140 --> 00:05:01,970
On a PC that's Control+Z.

119
00:05:01,970 --> 00:05:03,530
So rather than editing my changes,

120
00:05:03,530 --> 00:05:05,675
I'm gonna use Control + Z to undo them.

121
00:05:05,675 --> 00:05:08,980
So, firstly in main, we're going to undo the changes to Vlad

122
00:05:10,180 --> 00:05:11,820
to get rid of the extra line there

123
00:05:11,820 --> 00:05:14,490
that was passing the number of hit points.

124
00:05:14,490 --> 00:05:16,420
Then back in the Vampyre class,

125
00:05:16,420 --> 00:05:18,370
let's undo those changes as well.

126
00:05:18,370 --> 00:05:23,370
We'll remove hitPoints, and the default value as well,

127
00:05:23,970 --> 00:05:25,070
and that will mean that when we go back

128
00:05:25,070 --> 00:05:27,500
to the VampyreKing class, of course we've now got an error,

129
00:05:27,500 --> 00:05:29,950
because we're using the 140.

130
00:05:29,950 --> 00:05:31,610
So I'm going to undo that as well,

131
00:05:31,610 --> 00:05:34,020
so that we have it back to how we had it before.

132
00:05:34,020 --> 00:05:35,650
So how can we now set the hit points

133
00:05:35,650 --> 00:05:38,523
for our VampyreKing to 140?

134
00:05:39,430 --> 00:05:42,550
Well, what we can do instead, is to use it in an init block

135
00:05:42,550 --> 00:05:45,990
in VampyreKing to change the hit points value.

136
00:05:45,990 --> 00:05:50,990
So we can type in init, then left and right curly braces,

137
00:05:51,470 --> 00:05:53,920
and within the code block,

138
00:05:53,920 --> 00:05:56,560
we can assign hitPoints equal to 140.

139
00:05:56,560 --> 00:05:59,470
Now what does does is changes the value of hit points

140
00:05:59,470 --> 00:06:01,662
when an object gets created.

141
00:06:01,662 --> 00:06:04,970
Now init code's executed at the time

142
00:06:04,970 --> 00:06:06,550
that we create the instance.

143
00:06:06,550 --> 00:06:08,200
And it's done automatically,

144
00:06:08,200 --> 00:06:10,820
so we don't have to remember to do anything special.

145
00:06:10,820 --> 00:06:11,870
So if we run this programme,

146
00:06:11,870 --> 00:06:14,483
given that we've deleted those changes,

147
00:06:18,340 --> 00:06:20,620
we can see that Dracula is now still set at

148
00:06:20,620 --> 00:06:23,080
getting his hit points set to 140,

149
00:06:23,080 --> 00:06:26,283
but we haven't compromised the Vampyre class in any way.

150
00:06:27,250 --> 00:06:29,050
Basically, we've extended the Vampyre class

151
00:06:29,050 --> 00:06:31,940
and changed the hit points for our VampyreKing,

152
00:06:31,940 --> 00:06:35,360
without having to modify the VampyreKing's superclass.

153
00:06:35,360 --> 00:06:37,210
Alright, so all that remains now

154
00:06:37,210 --> 00:06:39,580
is to override the takeDamage function

155
00:06:39,580 --> 00:06:41,790
to divide the damage by four.

156
00:06:41,790 --> 00:06:43,870
Now, if you recall, I've already done something similar,

157
00:06:43,870 --> 00:06:47,920
with the Vampyre class, I used the takeDamage,

158
00:06:47,920 --> 00:06:49,940
so let's do something similar here.

159
00:06:49,940 --> 00:06:54,373
Here, VampyreKing class, and then override takeDamage,

160
00:06:56,798 --> 00:07:01,042
and what I'm going to do is set that damage

161
00:07:01,042 --> 00:07:02,467
into divided by two there.

162
00:07:03,360 --> 00:07:04,840
Remember that we're trying to overall

163
00:07:04,840 --> 00:07:07,090
change the damage by four,

164
00:07:07,090 --> 00:07:09,403
divide the total damage taken by four.

165
00:07:10,490 --> 00:07:12,430
And in fact, that's exactly the same code now

166
00:07:12,430 --> 00:07:14,590
that we've got into the Vampyre class.

167
00:07:14,590 --> 00:07:17,680
That's the, if you recall, the Vampyre class,

168
00:07:17,680 --> 00:07:19,310
that's already setting the damage,

169
00:07:19,310 --> 00:07:21,240
dividing that damage by two.

170
00:07:21,240 --> 00:07:23,240
We're doing the same thing here in takeDamage

171
00:07:23,240 --> 00:07:25,030
for the VampyreKing class,

172
00:07:25,030 --> 00:07:28,163
so we're only dividing it again by two here and not by four.

173
00:07:29,190 --> 00:07:32,530
So it is important to understand exactly which class

174
00:07:32,530 --> 00:07:34,370
is the superclass here.

175
00:07:34,370 --> 00:07:36,830
Now, VampyreKing extends Vampyre,

176
00:07:36,830 --> 00:07:38,340
so the super method that we're calling

177
00:07:38,340 --> 00:07:41,650
is the Vampyre method, and not the enemy method.

178
00:07:41,650 --> 00:07:44,295
So what happens is that the VampyreKing takeDamage method

179
00:07:44,295 --> 00:07:47,971
calls the method in its superclass, which is Vampyre.

180
00:07:47,971 --> 00:07:50,550
Now the Vampyre class takeDamage method,

181
00:07:50,550 --> 00:07:53,120
it calls its superclass method, or function,

182
00:07:53,120 --> 00:07:56,630
so that it calls the enemy takeDamage function.

183
00:07:56,630 --> 00:07:59,500
Now Android Studio has a really useful feature

184
00:07:59,500 --> 00:08:01,270
to help understand all this.

185
00:08:01,270 --> 00:08:04,070
So back in the VampyreKing class, which I've got open,

186
00:08:04,070 --> 00:08:06,480
I can hold down the Command key, 'cause I'm on a Mac,

187
00:08:06,480 --> 00:08:08,130
but it's Control on a PC,

188
00:08:08,130 --> 00:08:10,790
and click on the takeDamage,

189
00:08:10,790 --> 00:08:12,856
in its call to super.takeDamage,

190
00:08:12,856 --> 00:08:15,560
and I click on that, and you can see that

191
00:08:15,560 --> 00:08:18,173
it's automatically jumped to the Vampyre class,

192
00:08:19,010 --> 00:08:22,700
or rather the takeDamage function in the Vampyre superclass.

193
00:08:22,700 --> 00:08:25,153
Now if I do that again, in this class,

194
00:08:26,490 --> 00:08:29,570
you can see it's now jumped to the takeDamage function

195
00:08:29,570 --> 00:08:31,420
in the enemy class.

196
00:08:31,420 --> 00:08:34,890
So basically that's the superclass of Vampyre.

197
00:08:34,890 --> 00:08:36,830
So you can follow the trail back,

198
00:08:36,830 --> 00:08:37,890
and that's great for checking

199
00:08:37,890 --> 00:08:41,789
exactly what these overridden functions are doing.

200
00:08:41,789 --> 00:08:44,190
Okay, so we only have to divide the damage by two

201
00:08:44,190 --> 00:08:45,893
in the VampyreKing class again,

202
00:08:46,810 --> 00:08:49,310
and that's because it'll be divided again

203
00:08:49,310 --> 00:08:51,580
when the Vampyre takeDamage method is called,

204
00:08:51,580 --> 00:08:53,380
in other words, when this code is executed,

205
00:08:53,380 --> 00:08:55,650
which is also going to be divided by two.

206
00:08:55,650 --> 00:08:56,820
So, let's see if it works,

207
00:08:56,820 --> 00:08:59,720
by inflicting some damage on Dracula in the main function.

208
00:09:01,830 --> 00:09:06,617
So we'll do dracula.takeDamage(12).

209
00:09:08,026 --> 00:09:08,976
And we'll run that.

210
00:09:13,960 --> 00:09:15,760
So when we run the programme, we can see that Dracula

211
00:09:15,760 --> 00:09:17,897
starts off with 140 hit points,

212
00:09:17,897 --> 00:09:19,890
but then inflicted 12 points of damage,

213
00:09:19,890 --> 00:09:21,760
but that gets divided by two

214
00:09:21,760 --> 00:09:24,430
in the VampyreKing's takeDamage method,

215
00:09:24,430 --> 00:09:26,520
then divided by two again in the Vampyre class.

216
00:09:26,520 --> 00:09:29,110
So we only inflict a total of three hit points

217
00:09:29,110 --> 00:09:30,810
of damage on Dracula.

218
00:09:30,810 --> 00:09:35,000
So Dracula ends up with 137 hit points left,

219
00:09:35,000 --> 00:09:37,233
three points of damage, which is correct.

220
00:09:38,330 --> 00:09:39,637
Alright, so I'm gonna stop the video here,

221
00:09:39,637 --> 00:09:41,860
and in the next one, we're gonna look at using loops

222
00:09:41,860 --> 00:09:43,760
to repeat a section of code.

223
00:09:43,760 --> 00:09:44,760
We're actually gonna use a loop

224
00:09:44,760 --> 00:09:47,260
to repeatedly inflict damage on Dracula,

225
00:09:47,260 --> 00:09:49,720
so that hopefully, we can slay that brute.

226
00:09:49,720 --> 00:09:51,020
See you in the next video.

