1
00:00:04,720 --> 00:00:06,550
Alright, so let's continue

2
00:00:06,550 --> 00:00:09,010
looking at class inheritance. Now I'm gonna

3
00:00:09,010 --> 00:00:10,870
start this video by creating another

4
00:00:10,870 --> 00:00:12,700
enemy, this time we're gonna have a

5
00:00:12,700 --> 00:00:15,340
vampire, so go over here and right-click

6
00:00:15,340 --> 00:00:17,800
on the Java folder select new Kotlin

7
00:00:17,800 --> 00:00:19,539
file class and we're going to choose

8
00:00:19,539 --> 00:00:22,750
vampire. Now by the way if you want to

9
00:00:22,750 --> 00:00:24,040
save a bit of typing, I haven't been

10
00:00:24,040 --> 00:00:25,840
doing that you can select a class and

11
00:00:25,840 --> 00:00:27,910
that just adds that first line for us

12
00:00:27,910 --> 00:00:30,010
just to save a little bit of typing.

13
00:00:30,010 --> 00:00:31,690
Alright, so our vampires will have three

14
00:00:31,690 --> 00:00:34,750
lives and take 20 points of damage.

15
00:00:34,750 --> 00:00:37,140
So let's start with parentheses after the

16
00:00:37,140 --> 00:00:42,519
vampire the name colon string, then we

17
00:00:42,519 --> 00:00:45,960
want to call a space colon space enemy

18
00:00:45,960 --> 00:00:50,440
parenthesis name comma space 20 comma 3

19
00:00:50,440 --> 00:00:52,890
and there's our definition for vampire.

20
00:00:52,890 --> 00:00:55,539
Now let's create a vampire in our main

21
00:00:55,539 --> 00:00:58,050
function and check that it works okay

22
00:00:58,050 --> 00:01:00,399
then what I'm going to do is delete this

23
00:01:00,399 --> 00:01:02,649
enemy code, this test code because we're

24
00:01:02,649 --> 00:01:08,380
no longer need that anymore. So that's

25
00:01:08,380 --> 00:01:10,660
some code after the ugly troll, the ugly

26
00:01:10,660 --> 00:01:13,200
troll so I'm going to start with val

27
00:01:13,200 --> 00:01:20,200
flat equals vampire parenthesis his name

28
00:01:20,200 --> 00:01:24,000
is Vlad we'll print it out print out Vlad

29
00:01:24,000 --> 00:01:26,620
then we'll also do a test for some

30
00:01:26,620 --> 00:01:28,720
damage, so he led Vlad rather dot take

31
00:01:28,720 --> 00:01:34,000
damage eight and print 'ln Vlad again.

32
00:01:34,000 --> 00:01:38,250
Let's check that that's working right

33
00:01:38,250 --> 00:01:41,410
see the troll is dead but the vampire

34
00:01:41,410 --> 00:01:44,050
started with 20 points lost eight points

35
00:01:44,050 --> 00:01:45,910
and there's 12 left and showing 12

36
00:01:45,910 --> 00:01:47,860
points okay, so that's working fine.

37
00:01:47,860 --> 00:01:50,170
Now wouldn't think we know about vampires is

38
00:01:50,170 --> 00:01:52,750
that they were notoriously difficult to

39
00:01:52,750 --> 00:01:55,210
kill so this is a good opportunity to

40
00:01:55,210 --> 00:01:57,220
look at overwriting functions in a

41
00:01:57,220 --> 00:02:00,130
subclass. Now when we override a function

42
00:02:00,130 --> 00:02:02,590
we provide a different implementation to

43
00:02:02,590 --> 00:02:05,350
the function in the superclass. Now we've

44
00:02:05,350 --> 00:02:07,720
seen this before when we created new to

45
00:02:07,720 --> 00:02:09,788
string functions in our classes and this

46
00:02:09,788 --> 00:02:11,709
is exactly the same. Now what may not

47
00:02:11,709 --> 00:02:13,960
have been obvious is that all classes in

48
00:02:13,960 --> 00:02:16,780
Kotlin have a basic class, now if we

49
00:02:16,780 --> 00:02:18,490
don't specify a base class

50
00:02:18,490 --> 00:02:20,590
then our classes inherit from a class

51
00:02:20,590 --> 00:02:24,070
called any. Now it's the any class that

52
00:02:24,070 --> 00:02:26,410
provides things like to string that we

53
00:02:26,410 --> 00:02:27,430
overrode earlier.

54
00:02:27,430 --> 00:02:30,550
So in Kotlin, the topmost superclass the

55
00:02:30,550 --> 00:02:32,740
base class for all other classes is

56
00:02:32,740 --> 00:02:35,860
called any. Now our enemy class inherits

57
00:02:35,860 --> 00:02:38,410
from any and our vampire class inherits

58
00:02:38,410 --> 00:02:41,530
from enemy. Now because enemy overrides

59
00:02:41,530 --> 00:02:43,960
the to string function our trolls and

60
00:02:43,960 --> 00:02:46,870
vampires use the overridden version, so

61
00:02:46,870 --> 00:02:49,150
it's not a chain when we call a function

62
00:02:49,150 --> 00:02:51,550
of a class Kotlin checks to see if the

63
00:02:51,550 --> 00:02:54,250
function is defined in the class if it

64
00:02:54,250 --> 00:02:56,500
isn't Kotlin checks to superclass and

65
00:02:56,500 --> 00:02:58,690
executes the function in there if there

66
00:02:58,690 --> 00:03:00,760
is one. Now if that doesn't exist in the

67
00:03:00,760 --> 00:03:02,830
superclass either then that class's

68
00:03:02,830 --> 00:03:04,480
superclass will be checked and that

69
00:03:04,480 --> 00:03:06,670
keeps going up the chain of inheritance

70
00:03:06,670 --> 00:03:10,120
until a matching functions found and if

71
00:03:10,120 --> 00:03:11,770
it's not found in any of the super

72
00:03:11,770 --> 00:03:14,200
classes then any will be used eventually.

73
00:03:14,200 --> 00:03:16,390
Now of course if the function doesn't

74
00:03:16,390 --> 00:03:18,370
exist in the any class either, then we'll

75
00:03:18,370 --> 00:03:20,770
get an error. So let's see how that works

76
00:03:20,770 --> 00:03:23,380
I'm going to give our vampire class its

77
00:03:23,380 --> 00:03:26,650
own take damage method, so making sure

78
00:03:26,650 --> 00:03:28,690
that we'll go back to our vampire class

79
00:03:28,690 --> 00:03:32,320
making sure that she curses in the curly

80
00:03:32,320 --> 00:03:34,030
braces, so I since all the class

81
00:03:34,030 --> 00:03:36,250
another word and it's before the final

82
00:03:36,250 --> 00:03:37,990
curly brace and we can actually get

83
00:03:37,990 --> 00:03:40,510
Android studio to create the function

84
00:03:40,510 --> 00:03:43,030
stub for us. Now this isn't going to work

85
00:03:43,030 --> 00:03:44,440
straight away and your mini challenge

86
00:03:44,440 --> 00:03:46,630
will be to work out why but I'm jumping

87
00:03:46,630 --> 00:03:49,000
ahead there. So command n on a Mac or alt

88
00:03:49,000 --> 00:03:53,020
insert on a PC will produce this menu

89
00:03:53,020 --> 00:03:55,570
just generate context menu and we can

90
00:03:55,570 --> 00:03:57,450
choose the option to override methods

91
00:03:57,450 --> 00:04:00,280
I'm going to do that and by the way you

92
00:04:00,280 --> 00:04:02,200
can also use the generate option from

93
00:04:02,200 --> 00:04:06,250
the code menu if you prefer. Now we get

94
00:04:06,250 --> 00:04:08,650
this dialog showing all the methods in

95
00:04:08,650 --> 00:04:10,960
the enemy class, so we can choose which

96
00:04:10,960 --> 00:04:13,480
one to override and we can also get the

97
00:04:13,480 --> 00:04:15,790
methods we also see the methods from the

98
00:04:15,790 --> 00:04:17,730
any class superclass that I've mentioned.

99
00:04:17,730 --> 00:04:20,290
Now every class in Kotlin extends the

100
00:04:20,290 --> 00:04:22,570
any class as I mentioned so any is at

101
00:04:22,570 --> 00:04:25,120
the very top of the class hierarchy.

102
00:04:25,120 --> 00:04:26,890
Now that's quite useful because if you

103
00:04:26,890 --> 00:04:28,630
create a function that takes an any

104
00:04:28,630 --> 00:04:31,030
parameter then you can pass an instance

105
00:04:31,030 --> 00:04:32,710
of any class to.

106
00:04:32,710 --> 00:04:35,050
Now that's what I hinted at what I said

107
00:04:35,050 --> 00:04:36,910
that the ArrayList could store any type

108
00:04:36,910 --> 00:04:40,210
of object because all classes inherit

109
00:04:40,210 --> 00:04:42,460
from the any class and you can use any

110
00:04:42,460 --> 00:04:45,970
class type where any is expected. Now if

111
00:04:45,970 --> 00:04:47,920
you don't see this hierarchy in the

112
00:04:47,920 --> 00:04:50,680
dialog, click the see button to organize

113
00:04:50,680 --> 00:04:53,800
the functions in their classes. Alright,

114
00:04:53,800 --> 00:04:55,570
so the mini challenge, we want to

115
00:04:55,570 --> 00:04:57,730
overwrite the enemy class take damage

116
00:04:57,730 --> 00:04:59,770
function but you can see at the moment

117
00:04:59,770 --> 00:05:02,230
is not appearing in the list so your

118
00:05:02,230 --> 00:05:04,660
many challenges to work out why not, what

119
00:05:04,660 --> 00:05:06,610
haven't we done to make take damage

120
00:05:06,610 --> 00:05:09,550
available. Now ever think about that for a minute,

121
00:05:09,550 --> 00:05:12,040
now think back to what we had to do with

122
00:05:12,040 --> 00:05:14,140
our enemy class before we could inherit

123
00:05:14,140 --> 00:05:16,090
from it don't spend too long on this a

124
00:05:16,090 --> 00:05:17,890
minute or two should be enough, the aim

125
00:05:17,890 --> 00:05:19,990
is just to get you to think about it and

126
00:05:19,990 --> 00:05:22,120
also to remember the reason so that

127
00:05:22,120 --> 00:05:23,740
you're not caught out by it in the

128
00:05:23,740 --> 00:05:26,110
future. Alright, so pause the video

129
00:05:26,110 --> 00:05:27,550
there see if you can complete that

130
00:05:27,550 --> 00:05:28,930
mini-challenge and I'll see you when you

131
00:05:28,930 --> 00:05:32,800
get back. Alright so the reason that take

132
00:05:32,800 --> 00:05:34,840
damage isn't appearing here is that

133
00:05:34,840 --> 00:05:36,580
Kotlin doesn't allow functions to be

134
00:05:36,580 --> 00:05:39,130
overridden unless we explicitly mark

135
00:05:39,130 --> 00:05:41,290
them with open and that's the same as

136
00:05:41,290 --> 00:05:42,550
what we had to do with the enemy class

137
00:05:42,550 --> 00:05:44,740
before we could inherit from it, so I'm

138
00:05:44,740 --> 00:05:46,720
going to close this dialogue, we're gonna

139
00:05:46,720 --> 00:05:48,880
mark the take damage enemy function as

140
00:05:48,880 --> 00:05:56,320
open you know we go back and do that so

141
00:05:56,320 --> 00:05:59,320
go back now to our vampire class I'm

142
00:05:59,320 --> 00:06:01,090
doing a command in or it's an alt insert

143
00:06:01,090 --> 00:06:03,820
on a PC to get our generate menu I'm

144
00:06:03,820 --> 00:06:06,550
going to select override methods and

145
00:06:06,550 --> 00:06:07,840
this time you can see down under enemy

146
00:06:07,840 --> 00:06:10,390
take damage does appear along with the

147
00:06:10,390 --> 00:06:11,830
other functions that our vampire class

148
00:06:11,830 --> 00:06:13,960
inherits. So hopefully this shows the

149
00:06:13,960 --> 00:06:16,330
hierarchy vampires inheriting from an

150
00:06:16,330 --> 00:06:18,220
enemy because enemy automatically

151
00:06:18,220 --> 00:06:21,010
inherits any and vampires also

152
00:06:21,010 --> 00:06:23,650
inheriting the functions in any so we

153
00:06:23,650 --> 00:06:25,230
want to override the take damage

154
00:06:25,230 --> 00:06:27,490
function so going to select n if it's

155
00:06:27,490 --> 00:06:29,200
not already selected in my case it is,

156
00:06:29,200 --> 00:06:32,260
click on ok and you can see the code has

157
00:06:32,260 --> 00:06:34,840
been written for us. Now what we're going

158
00:06:34,840 --> 00:06:36,760
to do here is make our vampires only

159
00:06:36,760 --> 00:06:39,250
take half the damage that's inflicted on

160
00:06:39,250 --> 00:06:41,260
them, so if you're using a super

161
00:06:41,260 --> 00:06:43,060
warhammer that normally inflicts 20

162
00:06:43,060 --> 00:06:46,569
points of damage and vampire I'll only take

163
00:06:46,569 --> 00:06:48,939
10 point. So how do we go about doing that

164
00:06:48,939 --> 00:06:51,339
well we can actually change this damage

165
00:06:51,339 --> 00:06:56,379
and simply alter that divided by two so

166
00:06:56,379 --> 00:06:59,499
it does half as much damage. Now there

167
00:06:59,499 --> 00:07:01,300
are a few things to note about that but

168
00:07:01,300 --> 00:07:02,860
let's run it first to make sure that it

169
00:07:02,860 --> 00:07:05,589
works, we go back to our main remembering

170
00:07:05,589 --> 00:07:07,539
last time that lad took 8 points of

171
00:07:07,539 --> 00:07:09,369
damage and had 12 left you'll run it

172
00:07:09,369 --> 00:07:15,219
again now, you see this time that only

173
00:07:15,219 --> 00:07:17,879
took 4 points of damage and has 16 left.

174
00:07:17,879 --> 00:07:20,949
Okay, so back to my vampire class so let's

175
00:07:20,949 --> 00:07:23,529
see what we've done, now there's a lot of

176
00:07:23,529 --> 00:07:25,899
fancy words in programming and normally

177
00:07:25,899 --> 00:07:27,369
they just referred a pretty simple

178
00:07:27,369 --> 00:07:29,680
concepts. Now all we're doing here when

179
00:07:29,680 --> 00:07:32,309
we override the take damage function is

180
00:07:32,309 --> 00:07:34,509
creating a function with exactly the

181
00:07:34,509 --> 00:07:36,729
same name take damage and the same

182
00:07:36,729 --> 00:07:39,249
parameters in this case in it as the

183
00:07:39,249 --> 00:07:41,740
function in the superclass and when we

184
00:07:41,740 --> 00:07:43,809
call the ugly troll take damage function,

185
00:07:43,809 --> 00:07:45,939
the Java Runtime looks in the troll

186
00:07:45,939 --> 00:07:48,669
class for a function with that name when

187
00:07:48,669 --> 00:07:50,499
it doesn't find one and then starts

188
00:07:50,499 --> 00:07:52,119
going up the class hierarchy checking

189
00:07:52,119 --> 00:07:53,919
each class that troll extends to see if

190
00:07:53,919 --> 00:07:55,479
there's a take damage function in those

191
00:07:55,479 --> 00:07:57,729
classes and it finds one in the enemy

192
00:07:57,729 --> 00:07:59,860
class and so it runs that function,

193
00:07:59,860 --> 00:08:02,740
you know in our vampire class it finds to

194
00:08:02,740 --> 00:08:05,110
take damage function straight away, so it

195
00:08:05,110 --> 00:08:06,909
runs the one in the vampire class and

196
00:08:06,909 --> 00:08:09,039
doesn't bother checking the superclass.

197
00:08:09,039 --> 00:08:11,680
So we're creating a take damage function

198
00:08:11,680 --> 00:08:14,740
in the vampire class that divides the

199
00:08:14,740 --> 00:08:18,099
damage inflicted by two. Now the easiest

200
00:08:18,099 --> 00:08:20,259
way to implement that in this case is

201
00:08:20,259 --> 00:08:22,119
just to pass half the damage to the

202
00:08:22,119 --> 00:08:24,669
super classes take damage function so

203
00:08:24,669 --> 00:08:26,469
that's what I've done here on line seven,

204
00:08:26,469 --> 00:08:28,930
when an overriding function needs to

205
00:08:28,930 --> 00:08:31,050
call the function it's overriding,

206
00:08:31,050 --> 00:08:33,490
it prefixes the method name or the function

207
00:08:33,490 --> 00:08:36,610
name with super, so that's saying call

208
00:08:36,610 --> 00:08:38,438
the take damage function in the class

209
00:08:38,438 --> 00:08:41,318
that's being inherited from. Now it's very

210
00:08:41,318 --> 00:08:43,120
common for an overriding function to

211
00:08:43,120 --> 00:08:44,860
call the super function as we're doing

212
00:08:44,860 --> 00:08:47,829
here, but you don't have to we could have

213
00:08:47,829 --> 00:08:49,509
duplicated all the code from the enemy

214
00:08:49,509 --> 00:08:51,750
class but just taken up half the damage

215
00:08:51,750 --> 00:08:54,880
but in general duplicating code isn't a

216
00:08:54,880 --> 00:08:56,829
good idea and it's more efficient to

217
00:08:56,829 --> 00:08:59,740
just call the super function instead

218
00:08:59,740 --> 00:09:01,330
Alright, so I'm going to stop the video

219
00:09:01,330 --> 00:09:02,800
here yes always so if you've got

220
00:09:02,800 --> 00:09:04,330
questions feel free to leave them in the

221
00:09:04,330 --> 00:09:06,880
Q&A section of this course and we'll get

222
00:09:06,880 --> 00:09:08,680
back to you as quickly as we can and

223
00:09:08,680 --> 00:09:12,720
with that said I'll see you in the next video.

