1 00:00:04,730 --> 00:00:06,830 Alright, so we ended the last video by 2 00:00:06,830 --> 00:00:09,139 creating our basic class restoring the 3 00:00:09,139 --> 00:00:11,330 name hit point and lives it's now time 4 00:00:11,330 --> 00:00:13,519 to override the two string function 5 00:00:13,519 --> 00:00:14,990 we're going to use this to print out the 6 00:00:14,990 --> 00:00:16,400 properties so that we don't have to keep 7 00:00:16,400 --> 00:00:18,020 writing long print statements to check 8 00:00:18,020 --> 00:00:19,580 that everything's working just like 9 00:00:19,580 --> 00:00:21,529 we've done for the other classes, let's 10 00:00:21,529 --> 00:00:22,309 go ahead and do that 11 00:00:22,309 --> 00:00:24,259 that's how it's going to be right 12 00:00:24,259 --> 00:00:29,059 override fun to string you can press 13 00:00:29,059 --> 00:00:30,500 into there the rest is done for us 14 00:00:30,500 --> 00:00:32,450 automatically we're gonna change the 15 00:00:32,450 --> 00:00:35,030 super two string to make that instead 16 00:00:35,030 --> 00:00:39,200 double quotes name : dollar name comma 17 00:00:39,200 --> 00:00:44,570 hit points : dollar hit points chroma 18 00:00:44,570 --> 00:00:49,570 and then its lives : dollar lives 19 00:00:49,570 --> 00:00:52,579 alright that's very similar to stuff 20 00:00:52,579 --> 00:00:53,600 we've already covered in the earlier 21 00:00:53,600 --> 00:00:56,930 videos in this tutorial now we're not 22 00:00:56,930 --> 00:00:59,239 going to create enemy objects directly 23 00:00:59,239 --> 00:01:01,250 although we could but this is really 24 00:01:01,250 --> 00:01:04,220 just for use as a base class so that all 25 00:01:04,220 --> 00:01:06,259 the specific enemies have these basic 26 00:01:06,259 --> 00:01:08,749 properties so it's like a bird class you 27 00:01:08,749 --> 00:01:10,100 don't get animals that are just birds 28 00:01:10,100 --> 00:01:12,649 they're a particular type of bird so 29 00:01:12,649 --> 00:01:14,810 what I'm going to do is create one any 30 00:01:14,810 --> 00:01:16,700 enemy object just to test the class 31 00:01:16,700 --> 00:01:18,289 though to make sure that everything's 32 00:01:18,289 --> 00:01:20,090 working before we go extending the class 33 00:01:20,090 --> 00:01:23,299 now go back to the main function and we 34 00:01:23,299 --> 00:01:25,359 can actually test that by typing Val 35 00:01:25,359 --> 00:01:28,999 enemy is equal to enemy capital 36 00:01:28,999 --> 00:01:32,090 parentheses double quotes going to test 37 00:01:32,090 --> 00:01:34,700 enemy the second argument 38 00:01:34,700 --> 00:01:35,810 you can see there is hit points we're 39 00:01:35,810 --> 00:01:37,880 going to go with 10 with that and 3 for 40 00:01:37,880 --> 00:01:40,159 the number of lives and let's print the 41 00:01:40,159 --> 00:01:43,789 enemy out print them enemy okay let's 42 00:01:43,789 --> 00:01:48,799 run this and you can see we've got the 43 00:01:48,799 --> 00:01:49,939 output showing on the screen then 44 00:01:49,939 --> 00:01:51,499 everything seems doing working fine 45 00:01:51,499 --> 00:01:53,179 we've got the properties of our enemy 46 00:01:53,179 --> 00:01:55,340 object printing out all right so let's 47 00:01:55,340 --> 00:01:59,479 now test they take damage method so we 48 00:01:59,479 --> 00:02:02,599 can do something like enemy dot take 49 00:02:02,599 --> 00:02:06,770 damage for and we can print it out here 50 00:02:06,770 --> 00:02:14,160 me again so let's just run that 51 00:02:14,160 --> 00:02:15,990 and you can see that that's worked 52 00:02:15,990 --> 00:02:17,730 hitpoints was ten and it's now going 53 00:02:17,730 --> 00:02:20,520 down to six and obviously we're getting 54 00:02:20,520 --> 00:02:21,840 that message from the take damage 55 00:02:21,840 --> 00:02:24,300 function as well now we should also 56 00:02:24,300 --> 00:02:26,520 check what happens when we hit it really hard. 57 00:02:26,520 --> 00:02:28,440 So let's inflict eleven points of damage 58 00:02:28,440 --> 00:02:35,340 on this enemy object so enemy again dot 59 00:02:35,340 --> 00:02:39,450 take damage go for eleven this time and 60 00:02:39,450 --> 00:02:41,700 print out the message print out the 61 00:02:41,700 --> 00:02:48,050 values rather of the object run that 62 00:02:48,050 --> 00:02:50,190 testing that we lost to life as you can 63 00:02:50,190 --> 00:02:52,590 see and we're back to two lives as you 64 00:02:52,590 --> 00:02:55,290 can see there now you should always test 65 00:02:55,290 --> 00:02:57,660 all the paths through your code if you 66 00:02:57,660 --> 00:02:59,190 put in some conditions like we did when 67 00:02:59,190 --> 00:03:00,630 we check for the hit points dropping to 68 00:03:00,630 --> 00:03:03,030 zero or below then make sure you use 69 00:03:03,030 --> 00:03:05,040 data that will test the alternative path 70 00:03:05,040 --> 00:03:08,700 to make sure it works as you expect okay 71 00:03:08,700 --> 00:03:10,170 where it now gets interesting is when we 72 00:03:10,170 --> 00:03:13,050 look at extending this class let's have 73 00:03:13,050 --> 00:03:15,690 specific types of enemies because the 74 00:03:15,690 --> 00:03:17,160 code that I've currently got in that 75 00:03:17,160 --> 00:03:19,340 enemy class is of a very generic nature 76 00:03:19,340 --> 00:03:21,570 so let's assume that we've got different 77 00:03:21,570 --> 00:03:23,850 types of enemies for example we're going 78 00:03:23,850 --> 00:03:25,230 to start off by creating a new class 79 00:03:25,230 --> 00:03:27,690 called troll so one of the enemies our 80 00:03:27,690 --> 00:03:29,940 players will face in our game will be a 81 00:03:29,940 --> 00:03:32,610 troll so we're going to just we've done 82 00:03:32,610 --> 00:03:34,830 before right-click the Java class select 83 00:03:34,830 --> 00:03:37,620 new cut little file class call this one 84 00:03:37,620 --> 00:03:45,180 troll and we'll do class troll as we 85 00:03:45,180 --> 00:03:47,370 would normally create a class but our 86 00:03:47,370 --> 00:03:49,530 superclass is going to be an enemy so we 87 00:03:49,530 --> 00:03:51,330 need to specify that this troll class 88 00:03:51,330 --> 00:03:54,840 extends the enemy class now we do that 89 00:03:54,840 --> 00:03:56,730 by specifying the type of the sub the 90 00:03:56,730 --> 00:03:59,790 superclass after a colon now the 91 00:03:59,790 --> 00:04:01,350 convention is to leave a space before 92 00:04:01,350 --> 00:04:03,690 the colon as well so we would do 93 00:04:03,690 --> 00:04:06,900 something like this call them in the top 94 00:04:06,900 --> 00:04:10,410 enemy now we've got an error at the 95 00:04:10,410 --> 00:04:12,630 moment and if we hover over that this 96 00:04:12,630 --> 00:04:14,459 type has a constructor and thus must be 97 00:04:14,459 --> 00:04:17,100 initialized here what that means is we 98 00:04:17,100 --> 00:04:18,630 have to provide the properties that 99 00:04:18,630 --> 00:04:21,060 enemy needs now we do that by declaring 100 00:04:21,060 --> 00:04:23,520 variables in the sub class troll and 101 00:04:23,520 --> 00:04:26,690 passing them on to the superclass enemy 102 00:04:26,690 --> 00:04:29,600 so let's have a go at doing that I put 103 00:04:29,600 --> 00:04:34,480 parentheses after troll who named : 104 00:04:34,480 --> 00:04:41,360 string comma hit points : int and then 105 00:04:41,360 --> 00:04:46,370 lives : int and then we go over to enemy 106 00:04:46,370 --> 00:04:49,990 we had parentheses there and we put name 107 00:04:49,990 --> 00:04:54,170 comma space hit points come a space and 108 00:04:54,170 --> 00:04:57,320 lives. Okay you can see we've done 109 00:04:57,320 --> 00:04:59,540 undergoing error so what's happening 110 00:04:59,540 --> 00:05:01,010 there is that we'll provide values for 111 00:05:01,010 --> 00:05:02,840 the name hit points and lives when we 112 00:05:02,840 --> 00:05:05,690 create our troll. Now those values will 113 00:05:05,690 --> 00:05:07,550 be used to initialize the properties 114 00:05:07,550 --> 00:05:10,190 that we inherit from enemy now I've used 115 00:05:10,190 --> 00:05:12,110 the same names for those values as we've 116 00:05:12,110 --> 00:05:13,700 already used for the enemy properties 117 00:05:13,700 --> 00:05:15,860 that's fine because Contin can work out 118 00:05:15,860 --> 00:05:19,070 which are which from the context now 119 00:05:19,070 --> 00:05:20,810 there are other ways to write this code 120 00:05:20,810 --> 00:05:22,370 and we'll be seeing it done slightly 121 00:05:22,370 --> 00:05:25,250 differently later but this is probably 122 00:05:25,250 --> 00:05:27,650 the most straightforward but means we 123 00:05:27,650 --> 00:05:29,540 don't get a chance to modify the valleys 124 00:05:29,540 --> 00:05:31,370 that we pass on to the enemy constructor 125 00:05:31,370 --> 00:05:33,290 and that's something that we'll do shortly 126 00:05:33,290 --> 00:05:35,600 so to check that it works let's go back 127 00:05:35,600 --> 00:05:37,700 to the main function and create a troll 128 00:05:37,700 --> 00:05:39,830 class or create a trial object from the 129 00:05:39,830 --> 00:05:45,940 troll class, we're doing val ugly troll 130 00:05:45,940 --> 00:05:49,160 is equal to troll capital T parentheses 131 00:05:49,160 --> 00:05:54,320 the name will be ugly troll here looking 132 00:05:54,320 --> 00:05:57,730 about 27 hit points but only one live 133 00:05:57,730 --> 00:05:59,900 let's just print it out for print Len 134 00:05:59,900 --> 00:06:07,520 ugly troll let's run our code then we 135 00:06:07,520 --> 00:06:08,660 can either appearing on the same line 136 00:06:08,660 --> 00:06:09,860 cuz I didn't do a print Linden I did a 137 00:06:09,860 --> 00:06:12,140 print written in goes to the new line so 138 00:06:12,140 --> 00:06:13,520 let's just do that I'm running again I 139 00:06:13,520 --> 00:06:15,760 could see things a little bit easier 140 00:06:15,760 --> 00:06:17,840 that's better so you can see me cry and 141 00:06:17,840 --> 00:06:21,080 they ugly troll hit points 27 lines one 142 00:06:21,080 --> 00:06:23,510 that's working for the interesting thing 143 00:06:23,510 --> 00:06:24,710 is that we're able to print out the 144 00:06:24,710 --> 00:06:27,290 troll properties even though we didn't 145 00:06:27,290 --> 00:06:29,210 override to string in the troll class 146 00:06:29,210 --> 00:06:31,880 and that's because a subclass has access 147 00:06:31,880 --> 00:06:33,800 to all the properties and methods of its 148 00:06:33,800 --> 00:06:36,470 superclass so we can use to string and 149 00:06:36,470 --> 00:06:39,200 take damage for the trolls and for any 150 00:06:39,200 --> 00:06:40,490 other types of enemies that we 151 00:06:40,490 --> 00:06:42,110 create without having to write the 152 00:06:42,110 --> 00:06:44,449 functions again so let's actually check 153 00:06:44,449 --> 00:06:47,690 the take damage function so we'll go 154 00:06:47,690 --> 00:06:49,759 back to my B so I'm going to execute 155 00:06:49,759 --> 00:06:51,830 that so we'll go back to main and let's 156 00:06:51,830 --> 00:06:57,319 actually execute that now I control dot 157 00:06:57,319 --> 00:07:02,539 take damage eight and if we print that 158 00:07:02,539 --> 00:07:04,819 out just I'll make sure we should follow 159 00:07:04,819 --> 00:07:07,520 this had eight points of damage 160 00:07:07,520 --> 00:07:11,660 inflicted to it you can see exactly 161 00:07:11,660 --> 00:07:14,000 trial hit points nineteen now so 162 00:07:14,000 --> 00:07:15,530 basically that's working fire the trial 163 00:07:15,530 --> 00:07:17,090 took eight points of damage and has 164 00:07:17,090 --> 00:07:20,180 nineteen hit points left so you can see 165 00:07:20,180 --> 00:07:21,770 that we've got access to all the methods 166 00:07:21,770 --> 00:07:23,599 and properties of the enemies superclass 167 00:07:23,599 --> 00:07:25,400 without actually having to write all the 168 00:07:25,400 --> 00:07:27,979 code again now all our trials are going 169 00:07:27,979 --> 00:07:29,870 to be pretty much the same they're going 170 00:07:29,870 --> 00:07:31,190 to have different names so that we can 171 00:07:31,190 --> 00:07:32,750 tell them apart but they'll all have the 172 00:07:32,750 --> 00:07:34,729 same number of hit points and only one 173 00:07:34,729 --> 00:07:36,680 life so I'm going to modify the 174 00:07:36,680 --> 00:07:39,169 constructor of the troll class so let's 175 00:07:39,169 --> 00:07:43,009 go ahead and do that so the code itself 176 00:07:43,009 --> 00:07:45,650 the constructor code we're going to 177 00:07:45,650 --> 00:07:48,440 change that it's got a name at the 178 00:07:48,440 --> 00:07:50,030 moment string we're going to delete the 179 00:07:50,030 --> 00:07:54,710 hit points and lives here we come over 180 00:07:54,710 --> 00:07:55,969 here we're going to replace hit points 181 00:07:55,969 --> 00:07:59,750 and lives with 27 and once for 27 for 182 00:07:59,750 --> 00:08:02,539 hit points and one for lights so I've 183 00:08:02,539 --> 00:08:03,979 deleted the hit points and the lives 184 00:08:03,979 --> 00:08:06,349 parameters from our constructor so when 185 00:08:06,349 --> 00:08:08,840 we create new trolls in the game we just 186 00:08:08,840 --> 00:08:11,180 have to give them a name but the troll 187 00:08:11,180 --> 00:08:13,729 class extends enemy and then enemy needs 188 00:08:13,729 --> 00:08:15,530 those three pieces of information in its 189 00:08:15,530 --> 00:08:17,509 constructor so we're passing in a 190 00:08:17,509 --> 00:08:19,039 specific values that we want for hit 191 00:08:19,039 --> 00:08:20,930 points and lice as you can see that I've 192 00:08:20,930 --> 00:08:24,020 added there. So now when I create an 193 00:08:24,020 --> 00:08:25,610 control object in the main 194 00:08:25,610 --> 00:08:27,530 function we actually only need to 195 00:08:27,530 --> 00:08:29,180 provide the first argument and we 196 00:08:29,180 --> 00:08:30,770 getting this warning because we're 197 00:08:30,770 --> 00:08:32,000 putting too many arguments in there 198 00:08:32,000 --> 00:08:34,039 because we're providing more values than 199 00:08:34,039 --> 00:08:37,729 what it needs so let's remove those and 200 00:08:37,729 --> 00:08:39,469 we should also test the alternative path 201 00:08:39,469 --> 00:08:41,750 the alternative path through the take 202 00:08:41,750 --> 00:08:43,789 damage method so let's give our ugly 203 00:08:43,789 --> 00:08:45,350 troll a hefty whack with the sword 204 00:08:45,350 --> 00:08:47,779 glamdring and afflict 30 points of 205 00:08:47,779 --> 00:08:50,300 damage so we'll change the eight here to 206 00:08:50,300 --> 00:08:55,640 30 run that 207 00:08:55,640 --> 00:08:58,110 and you can say that the ugly troll lost 208 00:08:58,110 --> 00:09:00,450 the life. Now I mentioned that we should 209 00:09:00,450 --> 00:09:01,920 really be tracking the lights when 210 00:09:01,920 --> 00:09:03,329 they're lost if you try this yourself 211 00:09:03,329 --> 00:09:05,399 you can compare your solution with - I 212 00:09:05,399 --> 00:09:07,800 make this change. Now the take damage 213 00:09:07,800 --> 00:09:09,480 function is defined in the enemy class 214 00:09:09,480 --> 00:09:13,470 so we can make our changes in there so 215 00:09:13,470 --> 00:09:15,180 I'm going to do is what lives why does 216 00:09:15,180 --> 00:09:18,350 it call one let's do a test there so if 217 00:09:18,350 --> 00:09:21,500 life's greater than zero 218 00:09:21,500 --> 00:09:24,720 okay block and I'll just put that print 219 00:09:24,720 --> 00:09:33,480 in there with another else print 'ln no 220 00:09:33,480 --> 00:09:39,269 lice left on a name he's dead if you run 221 00:09:39,269 --> 00:09:45,329 the program again you can see what the 222 00:09:45,329 --> 00:09:46,890 message and their lives left ugly troll 223 00:09:46,890 --> 00:09:50,130 is dead. So that change that we've made 224 00:09:50,130 --> 00:09:52,380 will now apply to any classes that 225 00:09:52,380 --> 00:09:54,959 extent the enemy superclass and if we 226 00:09:54,959 --> 00:09:57,029 hadn't created enemies by sub classing 227 00:09:57,029 --> 00:09:58,560 we would have had to go through and make 228 00:09:58,560 --> 00:10:00,269 check the year same change in every 229 00:10:00,269 --> 00:10:03,000 class we created but this way we only 230 00:10:03,000 --> 00:10:05,760 have to make the change once. Alright 231 00:10:05,760 --> 00:10:06,810 so I'm going to stop the video here and 232 00:10:06,810 --> 00:10:11,269 we'll finish this discussion in the next video.