Override vs New6:07 with Jeremy McLain
Don't forget the override keyword or you'll see this compiler warning.
If you created some new types of towers, 0:00 you may have noticed that a common mistake is to forget to use the override keyword. 0:03 Let's take a moment to see what happens when we forget the override keyword. 0:08 I created a sniper tower and 0:13 I didn't add override when I created the accuracy property. 0:15 I'll compile and see what happens. 0:18 We don't get a compiler error, instead we get a compiler warning. 0:23 It says TreehouseDefense.SniperTower.Accuracy 0:28 hides inherited member TreehouseDefense.Tower.Accuracy. 0:34 To make the current member override that implementation add the override keyword. 0:39 Otherwise add the new keyword. 0:45 So what does it mean to hide a member and what is the new keyword? 0:47 It's very rare but it is possible to have two members that have the same signature. 0:53 But one doesn't replace the other. 0:58 Methods or properties with the same signature 1:00 have the exact same type of parameters and return the same type. 1:03 By default, if we don't add the override keyword, the member in the subclass 1:08 will just be an additional member that can be called on the class. 1:13 It's called hiding because the original member isn't being completely overridden. 1:17 This is hard to understand without seeing it in action. 1:23 Let's experiment by using the REPL a bit. 1:26 I'll create a class named Base. 1:31 That has a single public virtual string property named name. 1:35 And all it will do is return the word Base, And 1:44 I'll create another class named Sub that will be a subclass of the Base class. 1:51 And it also has a public string property named name. 2:00 But it will return the word sub. 2:06 Name is a read only property, and notice that I didn't type over right here. 2:13 And here's that warning again. 2:19 Let's create an instance of base and inspect name. 2:21 We'll say b.Name. 2:31 All right, we get back the word Base. 2:34 Let's do the same with the subclass. 2:37 As expected, we get back the word Sub. 2:42 Now let's see what happens when we assign an instance of Sub 2:46 to a variable of type Base. 2:51 We get the word Base. 2:55 Had we used the override keyword in the subclass, this would have printed 2:57 Sub because the name property was overridden in the subclass. 3:02 But because it isn't overridden, 3:07 it's just hidden, we can still access the name property of the Base class. 3:10 Let's contrast this to using override. 3:15 I'll rewrite the subclass. 3:18 This time we'll have the word override here and we'll do what we did before. 3:24 We'll create an instance of Sub and assign it to a variable of type Base. 3:32 And see what we get from the name. 3:41 This time, we get Sub. 3:44 Because the name property is not merely hidden, it's overridden. 3:46 Even casting a Sub to a Base, and 3:51 calling name on it can't give us the Base class's name. 3:54 See, we still get the word Sub. 3:59 More often than not we want to use override. 4:02 That's why we're getting the warning. 4:06 If we really did intend not to override the member, we should use the new keyword. 4:08 Using the new keyword has the same effect as not putting anything here at all. 4:13 But it gets rid of the warning. 4:18 When programming, we should always make our intentions clear, and by using the new 4:20 keyword we're saying, yes, I know that I'm not overwriting this member. 4:25 I'm creating an additional member with the same signature. 4:30 New is rarely used, 4:34 because in most cases we want to override the Base class members. 4:36 Overriding class members gives us the benefits of only having to write code 4:40 that knows about the Base class. 4:45 As we've seen in the treehouse defense game. 4:47 Using new is so rare but there are very few real world purposes for it. 4:50 So we won't go much deeper into using it. 4:55 I'm showing you so that now when you see the warning in the future, 4:58 you'll know that you probably just forgot to type override. 5:01 See the teacher's notes if you'd like to learn more. 5:05 We'll fix this code and put override here. 5:08 By making some of the properties and methods in our base invader and 5:14 power classes virtual we've been able to extend our 5:18 game by adding many more types of towers and invaders. 5:21 So far the play method still only knows about the base tower and invader classes. 5:25 That's what we want. 5:31 We don't want to change a lot of code each time we come up with a new type of invader 5:32 or tower. 5:37 If you haven't done so already, I encourage you to create 5:38 more types of invaders and towers in order to practice what we've learned so far. 5:42 Don't worry if the whole idea of polymorphism, and virtual, 5:47 and overwritten methods, still isn't entirely clear to you. 5:51 We're going to get much more practice using what we just learned. 5:54 In the next part of this course, we'll learn about a very common application 5:58 of polymorphism when we learn about the object class. 6:02 Let's keep going. 6:06
You need to sign up for Treehouse in order to download course files.Sign up