1
00:00:05,360 --> 00:00:06,600
alright so we finished up in the

2
00:00:06,600 --> 00:00:09,150
previous video talking about how the

3
00:00:09,150 --> 00:00:11,160
intellij refactoring didn't quite

4
00:00:11,160 --> 00:00:13,050
work when it came to some of these

5
00:00:13,050 --> 00:00:16,140
replacement fields and it would be quite

6
00:00:16,140 --> 00:00:18,540
a bit of work to do this manually to

7
00:00:18,540 --> 00:00:21,350
make these changes point here was that

8
00:00:21,350 --> 00:00:23,270
just wanted to draw your attention to a

9
00:00:23,270 --> 00:00:25,800
possible pitfall with refactoring data

10
00:00:25,800 --> 00:00:29,060
attributes but also in this video

11
00:00:29,060 --> 00:00:30,020
we're going to show you a quick way of

12
00:00:30,020 --> 00:00:32,220
dealing with them all the important

13
00:00:32,220 --> 00:00:33,890
thing here is that you can get problems

14
00:00:33,890 --> 00:00:35,280
if you use this form of replacement

15
00:00:35,280 --> 00:00:36,540
fields and then try to refactor the

16
00:00:36,540 --> 00:00:39,150
attributes later okay so we've used

17
00:00:39,150 --> 00:00:41,610
regular expressions in IntelliJ once

18
00:00:41,610 --> 00:00:44,280
before and their incredibly

19
00:00:44,280 --> 00:00:46,380
powerful and we'll be looking at them

20
00:00:46,380 --> 00:00:49,440
at using them in python code later

21
00:00:49,440 --> 00:00:51,660
in the course for now just follow along

22
00:00:51,660 --> 00:00:53,910
and will sort out these incorrect

23
00:00:53,910 --> 00:00:55,230
attribute names

24
00:00:55,230 --> 00:00:56,790
we're going to get intellij

25
00:00:56,790 --> 00:00:59,910
to do a search and replace and we can do that

26
00:00:59,910 --> 00:01:02,010
by pressing command on a Mac which I'm

27
00:01:02,010 --> 00:01:05,099
going to do or control r on a pc and if

28
00:01:05,099 --> 00:01:07,140
you need to it's the Edit menu find and

29
00:01:07,140 --> 00:01:11,760
replace find replace what i'm going to do

30
00:01:11,760 --> 00:01:16,020
an alt r this little window pops up

31
00:01:16,020 --> 00:01:19,050
they want to make sure that this reg ex

32
00:01:19,050 --> 00:01:20,940
is actually ticked regular

33
00:01:20,940 --> 00:01:23,370
expressions box in the top field we want

34
00:01:23,370 --> 00:01:26,490
to type left parenthesis we want to put

35
00:01:26,490 --> 00:01:32,760
a...

36
00:01:32,760 --> 00:01:35,700
...

37
00:01:35,700 --> 00:01:38,130
...and you can see

38
00:01:38,130 --> 00:01:40,320
as soon as I finish the first one it

39
00:01:40,320 --> 00:01:41,390
actually came up and matched a

40
00:01:41,390 --> 00:01:43,530
particular field there for us so we want

41
00:01:43,530 --> 00:01:45,600
to replace that we just need to type...

42
00:01:45,600 --> 00:01:50,370
so both the curly

43
00:01:50,370 --> 00:01:52,350
brace and the full stop in that first

44
00:01:52,350 --> 00:01:54,510
field are typed in have special meaning

45
00:01:54,510 --> 00:01:56,640
in regular expressions and

46
00:01:56,640 --> 00:01:57,710
that's why we having to use the

47
00:01:57,710 --> 00:02:00,810
backslash to escape them the backslash d

48
00:02:00,810 --> 00:02:03,570
matches any digit and again you can see

49
00:02:03,570 --> 00:02:05,520
that the occurrences are now showing up

50
00:02:05,520 --> 00:02:07,170
now that I've actually into that in this

51
00:02:07,170 --> 00:02:09,509
class obviously i needed to be an enemy

52
00:02:09,509 --> 00:02:11,940
.py before I started this and for the

53
00:02:11,940 --> 00:02:14,300
replacement in the second field and we

54
00:02:14,300 --> 00:02:16,170
just need to put the dollar sign one_

55
00:02:16,170 --> 00:02:16,640
...

56
00:02:16,640 --> 00:02:19,640
now here that dollar sign means or

57
00:02:19,640 --> 00:02:22,060
specifies anything that was matched

58
00:02:22,060 --> 00:02:24,110
inside the parentheses in the search box

59
00:02:24,110 --> 00:02:26,690
so when all set and done what we're doing

60
00:02:26,690 --> 00:02:28,460
is just adding an _ after the

61
00:02:28,460 --> 00:02:29,470
full stop

62
00:02:29,470 --> 00:02:32,890
then click on replace all and that takes

63
00:02:32,890 --> 00:02:35,590
care of all the replacement fields if

64
00:02:35,590 --> 00:02:36,830
you happen to have made a mistake there

65
00:02:36,830 --> 00:02:38,590
you can do a control z on a pc

66
00:02:38,590 --> 00:02:40,720
commands z on a mac or come up to

67
00:02:40,720 --> 00:02:43,060
and then undo and then try again so

68
00:02:43,060 --> 00:02:44,530
you can't really go wrong it will just

69
00:02:44,530 --> 00:02:46,360
undo those last changes for you

70
00:02:46,360 --> 00:02:48,500
so now that I've done that I can close

71
00:02:48,500 --> 00:02:49,610
down this window

72
00:02:49,610 --> 00:02:52,910
go back to main and should now run for

73
00:02:52,910 --> 00:02:55,720
us you can see it's now running again

74
00:02:55,720 --> 00:02:57,940
properly so it wasn't the end of the

75
00:02:57,940 --> 00:02:59,800
world but it can be a bit scary when

76
00:02:59,800 --> 00:03:02,140
you factor things with IntelliJ and

77
00:03:02,140 --> 00:03:04,840
your code suddenly breaks as this is a

78
00:03:04,840 --> 00:03:06,940
common reason for refactoring data

79
00:03:06,940 --> 00:03:09,080
attributes in your classes and that's

80
00:03:09,080 --> 00:03:10,670
this form of the replacement field is so

81
00:03:10,670 --> 00:03:12,830
useful i thought i'd show you what can

82
00:03:12,830 --> 00:03:14,210
but what can actually go wrong and

83
00:03:14,210 --> 00:03:16,000
possibly save you some grief in the future

84
00:03:16,000 --> 00:03:17,560
if that happens to you

85
00:03:17,560 --> 00:03:19,940
of course if would use the _ to

86
00:03:19,940 --> 00:03:21,770
start with we wouldn't have had a problem

87
00:03:21,770 --> 00:03:22,400
at all

88
00:03:22,400 --> 00:03:24,940
now we can still change the values and

89
00:03:24,940 --> 00:03:27,280
our program still does because this is

90
00:03:27,280 --> 00:03:29,930
python and nothing is truly private but

91
00:03:29,930 --> 00:03:32,330
there's now an indication that we should

92
00:03:32,330 --> 00:03:33,940
really be changing those attributes

93
00:03:33,940 --> 00:03:35,330
directly

94
00:03:35,330 --> 00:03:44,850
let's move over now to a challenge

95
00:03:44,850 --> 00:03:46,830
so to help consolidate your

96
00:03:46,830 --> 00:03:48,990
understanding of inheritance got a

97
00:03:48,990 --> 00:03:51,330
challenge for you to try out so the

98
00:03:51,330 --> 00:03:53,940
challenge is to try and create a vampire King

99
00:03:53,940 --> 00:03:57,750
subclass of vampire now a vampire king

100
00:03:57,750 --> 00:04:00,060
is going to be incredibly powerful and

101
00:04:00,060 --> 00:04:01,890
any points of damage inflicted will be

102
00:04:01,890 --> 00:04:05,250
divided by four vampire King objects

103
00:04:05,250 --> 00:04:10,970
will also start off with 140 hit points

104
00:04:10,970 --> 00:04:14,300
so extend the vampire class to create a

105
00:04:14,300 --> 00:04:16,490
new vampire King class with those

106
00:04:16,490 --> 00:04:18,860
additional properties then you want to

107
00:04:18,860 --> 00:04:21,399
test the new class by creating a new

108
00:04:21,399 --> 00:04:23,530
vampire King object and checking that it

109
00:04:23,530 --> 00:04:24,650
does start with a hundred and

110
00:04:24,650 --> 00:04:26,300
forty hit points and only takes a

111
00:04:26,300 --> 00:04:29,360
quarter of any damage inflicted alright

112
00:04:29,360 --> 00:04:31,070
so pause the video here and try that out

113
00:04:31,070 --> 00:04:34,730
and i'll see you when you get back

114
00:04:34,730 --> 00:04:37,790
ok so how did you come up with that one

115
00:04:37,790 --> 00:04:39,370
let's go through now and come up with a

116
00:04:39,370 --> 00:04:41,450
solution for this we're going to start

117
00:04:41,450 --> 00:04:46,760
by creating the new class i'm

118
00:04:46,760 --> 00:04:48,430
going to create that just below the

119
00:04:48,430 --> 00:04:54,050
vampire class is going to be class...

120
00:04:54,050 --> 00:04:59,900
....

121
00:04:59,900 --> 00:05:01,960
...

122
00:05:01,960 --> 00:05:06,710
...

123
00:05:06,710 --> 00:05:09,770
....

124
00:05:09,770 --> 00:05:14,240
...

125
00:05:14,240 --> 00:05:21,500
...

126
00:05:21,500 --> 00:05:26,650
...now this constructor will

127
00:05:26,650 --> 00:05:29,210
...create a new object that is exactly the same as a

128
00:05:29,210 --> 00:05:31,700
an ordinary vampire so we do need to set

129
00:05:31,700 --> 00:05:34,250
the hit point value after calling

130
00:05:34,250 --> 00:05:37,400
this init method on line 60 let's go

131
00:05:37,400 --> 00:05:38,810
ahead and do that so it's gonna be self

132
00:05:38,810 --> 00:05:42,110
....

133
00:05:42,110 --> 00:05:44,810
...so that's our

134
00:05:44,810 --> 00:05:47,020
constructor done we're going to test

135
00:05:47,020 --> 00:05:48,860
that in a moment but notice the single

136
00:05:48,860 --> 00:05:51,080
parameter we use when calling the

137
00:05:51,080 --> 00:05:54,290
super init method on line 60 now our

138
00:05:54,290 --> 00:05:57,320
vampire class is a subclass of enemy and

139
00:05:57,320 --> 00:06:00,650
vampire king is a subclass of vampire so

140
00:06:00,650 --> 00:06:02,510
the method that will be called when we use

141
00:06:02,510 --> 00:06:05,210
super init here is actually the

142
00:06:05,210 --> 00:06:07,730
vampire class init method and not the

143
00:06:07,730 --> 00:06:11,120
enemy init method now the init method in

144
00:06:11,120 --> 00:06:13,490
the vampire class will then call init

145
00:06:13,490 --> 00:06:16,270
in the enemy class so the calls will pass

146
00:06:16,270 --> 00:06:18,500
up the chain with each sub class

147
00:06:18,500 --> 00:06:20,240
calling the init method of its

148
00:06:20,240 --> 00:06:22,850
superclass ok so next we need to

149
00:06:22,850 --> 00:06:24,600
override the take damage method

150
00:06:24,600 --> 00:06:26,700
so that the damage is divided by

151
00:06:26,700 --> 00:06:29,210
four so let's create the damage

152
00:06:29,210 --> 00:06:32,360
methods so def....

153
00:06:32,360 --> 00:06:35,420
...

154
00:06:35,420 --> 00:06:42,200
...

155
00:06:42,200 --> 00:06:44,850
...so we are now dividing

156
00:06:44,850 --> 00:06:46,170
the damaged by 4 when we call that

157
00:06:46,170 --> 00:06:48,860
super method and used integer

158
00:06:48,860 --> 00:06:50,900
division here because we don't

159
00:06:50,900 --> 00:06:52,700
want to be subtracting fractional values

160
00:06:52,700 --> 00:06:54,740
from the hit points so if we try to

161
00:06:54,740 --> 00:06:57,050
inflict nine points of damage our vampire

162
00:06:57,050 --> 00:06:59,180
king will only take two points that's

163
00:06:59,180 --> 00:07:00,620
because integer division

164
00:07:00,620 --> 00:07:02,180
actually rounds down

165
00:07:02,180 --> 00:07:04,110
alright so what about the ability to

166
00:07:04,110 --> 00:07:06,480
dodge well its important to understand

167
00:07:06,480 --> 00:07:09,360
exactly which class is the superclass

168
00:07:09,360 --> 00:07:13,140
here now vampire King extends vampire so

169
00:07:13,140 --> 00:07:15,260
the super method we are calling is the

170
00:07:15,260 --> 00:07:18,020
vampire method and not the enemy method

171
00:07:18,020 --> 00:07:21,390
so what happens is that the vampire King

172
00:07:21,390 --> 00:07:24,360
take_damage method calls the

173
00:07:24,360 --> 00:07:26,850
method in its superclass which is vampire

174
00:07:26,850 --> 00:07:29,330
the vampire class take_damage

175
00:07:29,330 --> 00:07:31,140
method then calls its superclass

176
00:07:31,140 --> 00:07:34,170
method so it calls the enemy take_damage

177
00:07:34,170 --> 00:07:36,510
method now intelliJ has a

178
00:07:36,510 --> 00:07:38,700
really useful feature to help understand

179
00:07:38,700 --> 00:07:41,100
all of this back in the vampire King

180
00:07:41,100 --> 00:07:44,430
class i can hold down ctrl or command

181
00:07:44,430 --> 00:07:46,640
depending on your operating system and

182
00:07:46,640 --> 00:07:48,710
click on the word take damage in the

183
00:07:48,710 --> 00:07:50,510
super . take damage method down here

184
00:07:50,510 --> 00:07:54,420
and actually click on that now notice

185
00:07:54,420 --> 00:07:55,860
that the cursor has taken me to the

186
00:07:55,860 --> 00:07:57,950
take_damage method from the

187
00:07:57,950 --> 00:08:00,740
vampire class if i do that again now

188
00:08:00,740 --> 00:08:02,610
coming over here to the take damage

189
00:08:02,610 --> 00:08:04,140
that's now in the vampire class click on

190
00:08:04,140 --> 00:08:06,990
it's now moved up the cursor to the

191
00:08:06,990 --> 00:08:09,020
take_damage method which is

192
00:08:09,020 --> 00:08:12,320
now in the enemy class again ctrl or

193
00:08:12,320 --> 00:08:15,620
command is the key you click along with

194
00:08:15,620 --> 00:08:18,450
the actual method to get that to

195
00:08:18,450 --> 00:08:21,110
work on your operating system so

196
00:08:21,110 --> 00:08:22,860
basically you can follow the trail back

197
00:08:22,860 --> 00:08:25,550
which is great for checking exactly what

198
00:08:25,550 --> 00:08:27,710
those overwritten methods are doing

199
00:08:27,710 --> 00:08:30,620
alright so that's it let's now see if it

200
00:08:30,620 --> 00:08:34,049
works and go back to our main py file

201
00:08:34,049 --> 00:08:36,679
and we're going to comment out the code

202
00:08:36,679 --> 00:08:38,039
in the main program

203
00:08:38,039 --> 00:08:44,790
do that completely and leave it down the bottom

204
00:08:44,790 --> 00:08:46,820
instead code at the top here and what

205
00:08:46,820 --> 00:08:47,750
we're going to do is create a new

206
00:08:47,750 --> 00:08:50,100
vampire King class called Dracula

207
00:08:50,100 --> 00:08:57,480
Dracula...

208
00:08:57,480 --> 00:08:59,130
thing we need to do because the moment

209
00:08:59,130 --> 00:09:01,110
you can see the vampire is there we haven't

210
00:09:01,110 --> 00:09:03,240
imported this class new class we need to

211
00:09:03,240 --> 00:09:06,120
import vampire King like so and come back

212
00:09:06,120 --> 00:09:08,130
here and specify that's the vampire King

213
00:09:08,130 --> 00:09:10,350
class and we're going to call him

214
00:09:10,350 --> 00:09:14,160
Dracula....so lets write it out to print him first to

215
00:09:14,160 --> 00:09:15,780
make sure that the initial values are

216
00:09:15,780 --> 00:09:19,170
said and he has got 240 points right then

217
00:09:19,170 --> 00:09:23,510
Dracula starters take damage...

218
00:09:23,510 --> 00:09:25,110
that damage will be divided by 4

219
00:09:25,110 --> 00:09:26,310
so we should only get three points of

220
00:09:26,310 --> 00:09:31,130
damage....

221
00:09:31,130 --> 00:09:34,250
and see that it works so you can

222
00:09:34,250 --> 00:09:37,230
see that in this case you got started of

223
00:09:37,230 --> 00:09:39,960
with 140 points of damage but he actually

224
00:09:39,960 --> 00:09:42,480
dodge successfully and therefore didn't

225
00:09:42,480 --> 00:09:43,710
lose any hit points because I'm

226
00:09:43,710 --> 00:09:45,660
remembering this only a one chance in

227
00:09:45,660 --> 00:09:47,250
three the dodgers and avoids all the

228
00:09:47,250 --> 00:09:49,170
damage in this case he did Dodge

229
00:09:49,170 --> 00:09:52,760
so i need to run this again this time he

230
00:09:52,760 --> 00:09:54,060
has managed to take three points of

231
00:09:54,060 --> 00:09:57,540
damage 12/4 is 3 so he's left we still

232
00:09:57,540 --> 00:09:59,810
with three lives and 137 hitpoints

233
00:09:59,810 --> 00:10:01,680
alright so that's the challenge

234
00:10:01,680 --> 00:10:04,310
completed now your solution may well be

235
00:10:04,310 --> 00:10:06,060
different to mine and is always that's

236
00:10:06,060 --> 00:10:06,750
fine

237
00:10:06,750 --> 00:10:09,030
the important thing is that it works not

238
00:10:09,030 --> 00:10:10,530
that it looks exactly the same as have

239
00:10:10,530 --> 00:10:12,840
done here it's often said that if you

240
00:10:12,840 --> 00:10:14,310
ask 10 programmers to solve a problem

241
00:10:14,310 --> 00:10:16,530
you'll get in 11 different solutions

242
00:10:16,530 --> 00:10:18,840
so there's very very rarely only one way

243
00:10:18,840 --> 00:10:21,180
to write code so the main thing is if

244
00:10:21,180 --> 00:10:23,250
your code solution works then

245
00:10:23,250 --> 00:10:24,420
congratulations

246
00:10:24,420 --> 00:10:25,980
alright so hopefully that's now

247
00:10:25,980 --> 00:10:28,230
consolidated your understanding of

248
00:10:28,230 --> 00:10:30,660
subclassing and how classes can inherit

249
00:10:30,660 --> 00:10:33,780
from the superclass and in turn how they

250
00:10:33,780 --> 00:10:35,880
can also be the superclass of another

251
00:10:35,880 --> 00:10:37,680
class that extends them further

252
00:10:37,680 --> 00:10:40,530
inheritance can be a very useful feature

253
00:10:40,530 --> 00:10:42,840
but maybe because it's so often taught

254
00:10:42,840 --> 00:10:44,280
right at the start of course is an

255
00:10:44,280 --> 00:10:46,310
object-oriented programming many

256
00:10:46,310 --> 00:10:47,970
programmers feel they have to use

257
00:10:47,970 --> 00:10:50,010
inheritance they go out of their way to

258
00:10:50,010 --> 00:10:51,630
look for ways that they can sub

259
00:10:51,630 --> 00:10:53,250
class something to produce

260
00:10:53,250 --> 00:10:55,350
something else they need and that's

261
00:10:55,350 --> 00:10:57,480
often not a helpful approach especially

262
00:10:57,480 --> 00:11:00,630
in a language like Python in python if

263
00:11:00,630 --> 00:11:02,460
there's a need to use inheritance then it really

264
00:11:02,460 --> 00:11:05,010
should jump out at you you you wouldn't

265
00:11:05,010 --> 00:11:07,290
really have to go looking for it now all

266
00:11:07,290 --> 00:11:09,030
our enemies were enemies they really did

267
00:11:09,030 --> 00:11:11,160
share common traits and we intended to

268
00:11:11,160 --> 00:11:13,230
use the more all of them in a similar way as

269
00:11:13,230 --> 00:11:15,180
things to bash in vent our frustration

270
00:11:15,180 --> 00:11:16,920
so the inheritance relationship

271
00:11:16,920 --> 00:11:19,590
we created in the last few videos were

272
00:11:19,590 --> 00:11:21,840
probably a good way to create the troll

273
00:11:21,840 --> 00:11:25,350
the vampire the vampire King classes now

274
00:11:25,350 --> 00:11:26,730
python relies heavily on an

275
00:11:26,730 --> 00:11:28,230
object-oriented feature called

276
00:11:28,230 --> 00:11:30,360
polymorphism i'm going to have a look at

277
00:11:30,360 --> 00:11:32,520
that in the next video and find out what

278
00:11:32,520 --> 00:11:34,320
people mean when they say if it quacks

279
00:11:34,320 --> 00:11:35,280
like a duck

280
00:11:35,280 --> 00:11:41,630
so see you in the next video

