1 00:00:00,300 --> 00:00:03,050 If you created some new types of towers, 2 00:00:03,050 --> 00:00:08,660 you may have noticed that a common mistake is to forget to use the override keyword. 3 00:00:08,660 --> 00:00:13,200 Let's take a moment to see what happens when we forget the override keyword. 4 00:00:13,200 --> 00:00:15,130 I created a sniper tower and 5 00:00:15,130 --> 00:00:18,710 I didn't add override when I created the accuracy property. 6 00:00:18,710 --> 00:00:20,399 I'll compile and see what happens. 7 00:00:23,320 --> 00:00:28,538 We don't get a compiler error, instead we get a compiler warning. 8 00:00:28,538 --> 00:00:34,180 It says TreehouseDefense.SniperTower.Accuracy 9 00:00:34,180 --> 00:00:39,560 hides inherited member TreehouseDefense.Tower.Accuracy. 10 00:00:39,560 --> 00:00:45,230 To make the current member override that implementation add the override keyword. 11 00:00:45,230 --> 00:00:47,890 Otherwise add the new keyword. 12 00:00:47,890 --> 00:00:53,070 So what does it mean to hide a member and what is the new keyword? 13 00:00:53,070 --> 00:00:58,290 It's very rare but it is possible to have two members that have the same signature. 14 00:00:58,290 --> 00:01:00,960 But one doesn't replace the other. 15 00:01:00,960 --> 00:01:03,870 Methods or properties with the same signature 16 00:01:03,870 --> 00:01:08,810 have the exact same type of parameters and return the same type. 17 00:01:08,810 --> 00:01:13,370 By default, if we don't add the override keyword, the member in the subclass 18 00:01:13,370 --> 00:01:17,950 will just be an additional member that can be called on the class. 19 00:01:17,950 --> 00:01:23,740 It's called hiding because the original member isn't being completely overridden. 20 00:01:23,740 --> 00:01:26,850 This is hard to understand without seeing it in action. 21 00:01:26,850 --> 00:01:28,970 Let's experiment by using the REPL a bit. 22 00:01:31,660 --> 00:01:34,150 I'll create a class named Base. 23 00:01:35,950 --> 00:01:42,550 That has a single public virtual string property named name. 24 00:01:44,540 --> 00:01:51,350 And all it will do is return the word Base, And 25 00:01:51,350 --> 00:01:58,460 I'll create another class named Sub that will be a subclass of the Base class. 26 00:02:00,230 --> 00:02:05,850 And it also has a public string property named name. 27 00:02:06,970 --> 00:02:09,920 But it will return the word sub. 28 00:02:13,520 --> 00:02:19,850 Name is a read only property, and notice that I didn't type over right here. 29 00:02:19,850 --> 00:02:21,830 And here's that warning again. 30 00:02:21,830 --> 00:02:26,380 Let's create an instance of base and inspect name. 31 00:02:31,780 --> 00:02:34,447 We'll say b.Name. 32 00:02:34,447 --> 00:02:37,620 All right, we get back the word Base. 33 00:02:37,620 --> 00:02:39,370 Let's do the same with the subclass. 34 00:02:42,937 --> 00:02:46,840 As expected, we get back the word Sub. 35 00:02:46,840 --> 00:02:51,700 Now let's see what happens when we assign an instance of Sub 36 00:02:51,700 --> 00:02:53,870 to a variable of type Base. 37 00:02:55,940 --> 00:02:57,960 We get the word Base. 38 00:02:57,960 --> 00:03:02,500 Had we used the override keyword in the subclass, this would have printed 39 00:03:02,500 --> 00:03:07,630 Sub because the name property was overridden in the subclass. 40 00:03:07,630 --> 00:03:10,070 But because it isn't overridden, 41 00:03:10,070 --> 00:03:15,390 it's just hidden, we can still access the name property of the Base class. 42 00:03:15,390 --> 00:03:18,990 Let's contrast this to using override. 43 00:03:18,990 --> 00:03:20,850 I'll rewrite the subclass. 44 00:03:24,940 --> 00:03:32,080 This time we'll have the word override here and we'll do what we did before. 45 00:03:32,080 --> 00:03:36,820 We'll create an instance of Sub and assign it to a variable of type Base. 46 00:03:41,760 --> 00:03:43,250 And see what we get from the name. 47 00:03:44,420 --> 00:03:46,150 This time, we get Sub. 48 00:03:46,150 --> 00:03:51,790 Because the name property is not merely hidden, it's overridden. 49 00:03:51,790 --> 00:03:54,870 Even casting a Sub to a Base, and 50 00:03:54,870 --> 00:03:58,270 calling name on it can't give us the Base class's name. 51 00:03:59,820 --> 00:04:02,320 See, we still get the word Sub. 52 00:04:02,320 --> 00:04:06,430 More often than not we want to use override. 53 00:04:06,430 --> 00:04:08,310 That's why we're getting the warning. 54 00:04:08,310 --> 00:04:13,740 If we really did intend not to override the member, we should use the new keyword. 55 00:04:13,740 --> 00:04:18,800 Using the new keyword has the same effect as not putting anything here at all. 56 00:04:18,800 --> 00:04:20,379 But it gets rid of the warning. 57 00:04:20,379 --> 00:04:25,546 When programming, we should always make our intentions clear, and by using the new 58 00:04:25,546 --> 00:04:30,780 keyword we're saying, yes, I know that I'm not overwriting this member. 59 00:04:30,780 --> 00:04:34,490 I'm creating an additional member with the same signature. 60 00:04:34,490 --> 00:04:36,590 New is rarely used, 61 00:04:36,590 --> 00:04:40,760 because in most cases we want to override the Base class members. 62 00:04:40,760 --> 00:04:45,740 Overriding class members gives us the benefits of only having to write code 63 00:04:45,740 --> 00:04:47,700 that knows about the Base class. 64 00:04:47,700 --> 00:04:50,260 As we've seen in the treehouse defense game. 65 00:04:50,260 --> 00:04:55,560 Using new is so rare but there are very few real world purposes for it. 66 00:04:55,560 --> 00:04:58,310 So we won't go much deeper into using it. 67 00:04:58,310 --> 00:05:01,870 I'm showing you so that now when you see the warning in the future, 68 00:05:01,870 --> 00:05:05,310 you'll know that you probably just forgot to type override. 69 00:05:05,310 --> 00:05:08,270 See the teacher's notes if you'd like to learn more. 70 00:05:08,270 --> 00:05:10,690 We'll fix this code and put override here. 71 00:05:14,242 --> 00:05:18,107 By making some of the properties and methods in our base invader and 72 00:05:18,107 --> 00:05:21,143 power classes virtual we've been able to extend our 73 00:05:21,143 --> 00:05:25,135 game by adding many more types of towers and invaders. 74 00:05:25,135 --> 00:05:31,055 So far the play method still only knows about the base tower and invader classes. 75 00:05:31,055 --> 00:05:32,560 That's what we want. 76 00:05:32,560 --> 00:05:37,320 We don't want to change a lot of code each time we come up with a new type of invader 77 00:05:37,320 --> 00:05:37,830 or tower. 78 00:05:38,870 --> 00:05:42,110 If you haven't done so already, I encourage you to create 79 00:05:42,110 --> 00:05:46,280 more types of invaders and towers in order to practice what we've learned so far. 80 00:05:47,290 --> 00:05:51,130 Don't worry if the whole idea of polymorphism, and virtual, 81 00:05:51,130 --> 00:05:54,580 and overwritten methods, still isn't entirely clear to you. 82 00:05:54,580 --> 00:05:58,510 We're going to get much more practice using what we just learned. 83 00:05:58,510 --> 00:06:02,490 In the next part of this course, we'll learn about a very common application 84 00:06:02,490 --> 00:06:06,350 of polymorphism when we learn about the object class. 85 00:06:06,350 --> 00:06:07,060 Let's keep going.