Alright, so let's continue
looking at class inheritance. Now I'm gonna
start this video by creating another
enemy, this time we're gonna have a
vampire, so go over here and right-click
on the Java folder select new Kotlin
file class and we're going to choose
vampire. Now by the way if you want to
save a bit of typing, I haven't been
doing that you can select a class and
that just adds that first line for us
just to save a little bit of typing.
Alright, so our vampires will have three
lives and take 20 points of damage.
So let's start with parentheses after the
vampire the name colon string, then we
want to call a space colon space enemy
parenthesis name comma space 20 comma 3
and there's our definition for vampire.
Now let's create a vampire in our main
function and check that it works okay
then what I'm going to do is delete this
enemy code, this test code because we're
no longer need that anymore. So that's
some code after the ugly troll, the ugly
troll so I'm going to start with val
flat equals vampire parenthesis his name
is Vlad we'll print it out print out Vlad
then we'll also do a test for some
damage, so he led Vlad rather dot take
damage eight and print 'ln Vlad again.
Let's check that that's working right
see the troll is dead but the vampire
started with 20 points lost eight points
and there's 12 left and showing 12
points okay, so that's working fine.
Now wouldn't think we know about vampires is
that they were notoriously difficult to
kill so this is a good opportunity to
look at overwriting functions in a
subclass. Now when we override a function
we provide a different implementation to
the function in the superclass. Now we've
seen this before when we created new to
string functions in our classes and this
is exactly the same. Now what may not
have been obvious is that all classes in
Kotlin have a basic class, now if we
don't specify a base class
then our classes inherit from a class
called any. Now it's the any class that
provides things like to string that we
overrode earlier.
So in Kotlin, the topmost superclass the
base class for all other classes is
called any. Now our enemy class inherits
from any and our vampire class inherits
from enemy. Now because enemy overrides
the to string function our trolls and
vampires use the overridden version, so
it's not a chain when we call a function
of a class Kotlin checks to see if the
function is defined in the class if it
isn't Kotlin checks to superclass and
executes the function in there if there
is one. Now if that doesn't exist in the
superclass either then that class's
superclass will be checked and that
keeps going up the chain of inheritance
until a matching functions found and if
it's not found in any of the super
classes then any will be used eventually.
Now of course if the function doesn't
exist in the any class either, then we'll
get an error. So let's see how that works
I'm going to give our vampire class its
own take damage method, so making sure
that we'll go back to our vampire class
making sure that she curses in the curly
braces, so I since all the class
another word and it's before the final
curly brace and we can actually get
Android studio to create the function
stub for us. Now this isn't going to work
straight away and your mini challenge
will be to work out why but I'm jumping
ahead there. So command n on a Mac or alt
insert on a PC will produce this menu
just generate context menu and we can
choose the option to override methods
I'm going to do that and by the way you
can also use the generate option from
the code menu if you prefer. Now we get
this dialog showing all the methods in
the enemy class, so we can choose which
one to override and we can also get the
methods we also see the methods from the
any class superclass that I've mentioned.
Now every class in Kotlin extends the
any class as I mentioned so any is at
the very top of the class hierarchy.
Now that's quite useful because if you
create a function that takes an any
parameter then you can pass an instance
of any class to.
Now that's what I hinted at what I said
that the ArrayList could store any type
of object because all classes inherit
from the any class and you can use any
class type where any is expected. Now if
you don't see this hierarchy in the
dialog, click the see button to organize
the functions in their classes. Alright,
so the mini challenge, we want to
overwrite the enemy class take damage
function but you can see at the moment
is not appearing in the list so your
many challenges to work out why not, what
haven't we done to make take damage
available. Now ever think about that for a minute,
now think back to what we had to do with
our enemy class before we could inherit
from it don't spend too long on this a
minute or two should be enough, the aim
is just to get you to think about it and
also to remember the reason so that
you're not caught out by it in the
future. Alright, so pause the video
there see if you can complete that
mini-challenge and I'll see you when you
get back. Alright so the reason that take
damage isn't appearing here is that
Kotlin doesn't allow functions to be
overridden unless we explicitly mark
them with open and that's the same as
what we had to do with the enemy class
before we could inherit from it, so I'm
going to close this dialogue, we're gonna
mark the take damage enemy function as
open you know we go back and do that so
go back now to our vampire class I'm
doing a command in or it's an alt insert
on a PC to get our generate menu I'm
going to select override methods and
this time you can see down under enemy
take damage does appear along with the
other functions that our vampire class
inherits. So hopefully this shows the
hierarchy vampires inheriting from an
enemy because enemy automatically
inherits any and vampires also
inheriting the functions in any so we
want to override the take damage
function so going to select n if it's
not already selected in my case it is,
click on ok and you can see the code has
been written for us. Now what we're going
to do here is make our vampires only
take half the damage that's inflicted on
them, so if you're using a super
warhammer that normally inflicts 20
points of damage and vampire I'll only take
10 point. So how do we go about doing that
well we can actually change this damage
and simply alter that divided by two so
it does half as much damage. Now there
are a few things to note about that but
let's run it first to make sure that it
works, we go back to our main remembering
last time that lad took 8 points of
damage and had 12 left you'll run it
again now, you see this time that only
took 4 points of damage and has 16 left.
Okay, so back to my vampire class so let's
see what we've done, now there's a lot of
fancy words in programming and normally
they just referred a pretty simple
concepts. Now all we're doing here when
we override the take damage function is
creating a function with exactly the
same name take damage and the same
parameters in this case in it as the
function in the superclass and when we
call the ugly troll take damage function,
the Java Runtime looks in the troll
class for a function with that name when
it doesn't find one and then starts
going up the class hierarchy checking
each class that troll extends to see if
there's a take damage function in those
classes and it finds one in the enemy
class and so it runs that function,
you know in our vampire class it finds to
take damage function straight away, so it
runs the one in the vampire class and
doesn't bother checking the superclass.
So we're creating a take damage function
in the vampire class that divides the
damage inflicted by two. Now the easiest
way to implement that in this case is
just to pass half the damage to the
super classes take damage function so
that's what I've done here on line seven,
when an overriding function needs to
call the function it's overriding,
it prefixes the method name or the function
name with super, so that's saying call
the take damage function in the class
that's being inherited from. Now it's very
common for an overriding function to
call the super function as we're doing
here, but you don't have to we could have
duplicated all the code from the enemy
class but just taken up half the damage
but in general duplicating code isn't a
good idea and it's more efficient to
just call the super function instead
Alright, so I'm going to stop the video
here yes always so if you've got
questions feel free to leave them in the
Q&A section of this course and we'll get
back to you as quickly as we can and
with that said I'll see you in the next video.