Alright, so we ended the last video by
creating our basic class restoring the
name hit point and lives it's now time
to override the two string function
we're going to use this to print out the
properties so that we don't have to keep
writing long print statements to check
that everything's working just like
we've done for the other classes, let's
go ahead and do that
that's how it's going to be right
override fun to string you can press
into there the rest is done for us
automatically we're gonna change the
super two string to make that instead
double quotes name : dollar name comma
hit points : dollar hit points chroma
and then its lives : dollar lives
alright that's very similar to stuff
we've already covered in the earlier
videos in this tutorial now we're not
going to create enemy objects directly
although we could but this is really
just for use as a base class so that all
the specific enemies have these basic
properties so it's like a bird class you
don't get animals that are just birds
they're a particular type of bird so
what I'm going to do is create one any
enemy object just to test the class
though to make sure that everything's
working before we go extending the class
now go back to the main function and we
can actually test that by typing Val
enemy is equal to enemy capital
parentheses double quotes going to test
enemy the second argument
you can see there is hit points we're
going to go with 10 with that and 3 for
the number of lives and let's print the
enemy out print them enemy okay let's
run this and you can see we've got the
output showing on the screen then
everything seems doing working fine
we've got the properties of our enemy
object printing out all right so let's
now test they take damage method so we
can do something like enemy dot take
damage for and we can print it out here
me again so let's just run that
and you can see that that's worked
hitpoints was ten and it's now going
down to six and obviously we're getting
that message from the take damage
function as well now we should also
check what happens when we hit it really hard.
So let's inflict eleven points of damage
on this enemy object so enemy again dot
take damage go for eleven this time and
print out the message print out the
values rather of the object run that
testing that we lost to life as you can
see and we're back to two lives as you
can see there now you should always test
all the paths through your code if you
put in some conditions like we did when
we check for the hit points dropping to
zero or below then make sure you use
data that will test the alternative path
to make sure it works as you expect okay
where it now gets interesting is when we
look at extending this class let's have
specific types of enemies because the
code that I've currently got in that
enemy class is of a very generic nature
so let's assume that we've got different
types of enemies for example we're going
to start off by creating a new class
called troll so one of the enemies our
players will face in our game will be a
troll so we're going to just we've done
before right-click the Java class select
new cut little file class call this one
troll and we'll do class troll as we
would normally create a class but our
superclass is going to be an enemy so we
need to specify that this troll class
extends the enemy class now we do that
by specifying the type of the sub the
superclass after a colon now the
convention is to leave a space before
the colon as well so we would do
something like this call them in the top
enemy now we've got an error at the
moment and if we hover over that this
type has a constructor and thus must be
initialized here what that means is we
have to provide the properties that
enemy needs now we do that by declaring
variables in the sub class troll and
passing them on to the superclass enemy
so let's have a go at doing that I put
parentheses after troll who named :
string comma hit points : int and then
lives : int and then we go over to enemy
we had parentheses there and we put name
comma space hit points come a space and
lives. Okay you can see we've done
undergoing error so what's happening
there is that we'll provide values for
the name hit points and lives when we
create our troll. Now those values will
be used to initialize the properties
that we inherit from enemy now I've used
the same names for those values as we've
already used for the enemy properties
that's fine because Contin can work out
which are which from the context now
there are other ways to write this code
and we'll be seeing it done slightly
differently later but this is probably
the most straightforward but means we
don't get a chance to modify the valleys
that we pass on to the enemy constructor
and that's something that we'll do shortly
so to check that it works let's go back
to the main function and create a troll
class or create a trial object from the
troll class, we're doing val ugly troll
is equal to troll capital T parentheses
the name will be ugly troll here looking
about 27 hit points but only one live
let's just print it out for print Len
ugly troll let's run our code then we
can either appearing on the same line
cuz I didn't do a print Linden I did a
print written in goes to the new line so
let's just do that I'm running again I
could see things a little bit easier
that's better so you can see me cry and
they ugly troll hit points 27 lines one
that's working for the interesting thing
is that we're able to print out the
troll properties even though we didn't
override to string in the troll class
and that's because a subclass has access
to all the properties and methods of its
superclass so we can use to string and
take damage for the trolls and for any
other types of enemies that we
create without having to write the
functions again so let's actually check
the take damage function so we'll go
back to my B so I'm going to execute
that so we'll go back to main and let's
actually execute that now I control dot
take damage eight and if we print that
out just I'll make sure we should follow
this had eight points of damage
inflicted to it you can see exactly
trial hit points nineteen now so
basically that's working fire the trial
took eight points of damage and has
nineteen hit points left so you can see
that we've got access to all the methods
and properties of the enemies superclass
without actually having to write all the
code again now all our trials are going
to be pretty much the same they're going
to have different names so that we can
tell them apart but they'll all have the
same number of hit points and only one
life so I'm going to modify the
constructor of the troll class so let's
go ahead and do that so the code itself
the constructor code we're going to
change that it's got a name at the
moment string we're going to delete the
hit points and lives here we come over
here we're going to replace hit points
and lives with 27 and once for 27 for
hit points and one for lights so I've
deleted the hit points and the lives
parameters from our constructor so when
we create new trolls in the game we just
have to give them a name but the troll
class extends enemy and then enemy needs
those three pieces of information in its
constructor so we're passing in a
specific values that we want for hit
points and lice as you can see that I've
added there. So now when I create an
control object in the main
function we actually only need to
provide the first argument and we
getting this warning because we're
putting too many arguments in there
because we're providing more values than
what it needs so let's remove those and
we should also test the alternative path
the alternative path through the take
damage method so let's give our ugly
troll a hefty whack with the sword
glamdring and afflict 30 points of
damage so we'll change the eight here to
30 run that
and you can say that the ugly troll lost
the life. Now I mentioned that we should
really be tracking the lights when
they're lost if you try this yourself
you can compare your solution with - I
make this change. Now the take damage
function is defined in the enemy class
so we can make our changes in there so
I'm going to do is what lives why does
it call one let's do a test there so if
life's greater than zero
okay block and I'll just put that print
in there with another else print 'ln no
lice left on a name he's dead if you run
the program again you can see what the
message and their lives left ugly troll
is dead. So that change that we've made
will now apply to any classes that
extent the enemy superclass and if we
hadn't created enemies by sub classing
we would have had to go through and make
check the year same change in every
class we created but this way we only
have to make the change once. Alright
so I'm going to stop the video here and
we'll finish this discussion in the next video.