Virtual Properties4:37 with Jeremy McLain
Properties can be virtual too!
We've seen how methods can be overridden. 0:00 Properties can also be overridden. 0:02 Remember, properties are just syntactic sugar that combines a getter method 0:04 with the setter method into a single member that looks like a field. 0:09 We can override the implementation of properties just like methods. 0:13 To demonstrate, let's create another type of invader. 0:17 Let's create a new file and name it FastInvader. 0:21 Again, let's copy the namespace and class name from the invader 0:25 class. 0:29 Let's call this new class FastInvader, And have it be a subclass of Invader. 0:34 We'll also add a constructor. 0:43 And it will take a path property or a path parameter. 0:48 And it will pass this parameter up to the base class. 0:53 A fast invader will move further down the path each time its move method is called. 0:57 We could do this a couple of different ways. 1:02 In the invader's move method, we assume that when the move method is called, 1:04 the invader should move down the path by one step. 1:09 One way we could implement a FastInvader, is to make the move method in the invader 1:12 class virtual, and override it in the FastInvader class. 1:17 This is just like we did with the DecreaseHealth method. 1:21 Another way we could do this is to create a virtual property 1:25 that returns how many steps the invader should move and 1:29 have the move method use that lets see what this would look like. 1:33 So up here, we'll create a new property called StepSize. 1:37 It doesn't need a setter, it would just have a getter. 1:43 We'll have it return 1. 1:48 Since this property only has a getter, and 1:50 it returns a constant value, this is what's known as a read only property. 1:55 Another slightly simpler way to write a read only property is to write this. 2:00 The property only has a getter, and it's initialized to one. 2:06 Because it doesn't have a setter, it can't be changed to anything else. 2:10 Now down here in the move method, instead of one, we'll type StepSize. 2:14 Now, if we make this property virtual, we can override it in the FastInvader class. 2:22 Well, what should the access of this property be? 2:27 If we made it private, it would only be accessible from within the invader class, 2:30 not even the fast invader would be able to access it. 2:35 If we made it public, it would be accessible from anywhere. 2:38 This wouldn't necessarily be bad since it is after all, read only. 2:41 There's a third access level that gives us a happy compromise. 2:46 The protected access modifier allows access from only the invader class and 2:49 its sub classes. 2:55 That includes FastInvader, so we'll make this protected. 2:57 Let's copy this entire property and paste it in the FastInvader class. 3:01 Again, since we're in the sub class now, 3:08 we need to use the override keyword here instead of virtual. 3:10 And now, instead of having a StepSize of one, 3:13 FastInvader can have a StepSize of two. 3:16 Let's look at what happens when the move method is called on an invader or 3:20 FastInvader. 3:23 If move is called on a normal invader, the StepSize will be one. 3:25 So the invader will advance down the path by one step. 3:29 If the object is a FastInvader, StepSize will be two, 3:33 because it will be using FastInvader's implementation of the StepSize property, 3:37 instead of the original one. 3:42 With just a few lines of code, we were able to create a new type of invader. 3:44 And we don't need to touch any other code. 3:49 That's the power of inheritance encapsulation and 3:52 polymorphism that makes it so easy to maintain, update, and extend the game. 3:55 And now, because StepSize and 4:00 DecreasedHealth are both virtual members of the invader class. 4:01 If we wanted another type of invader that had a custom StepSize attribute or 4:06 DecreaseHealth behavior, we simply need to create a new class and 4:11 override the implementations for StepSize and DecreaseHealth. 4:15 We wouldn't even need to touch the base invader class again. 4:19 We can add FastInvader to our level. 4:22 Before we move on, 4:26 let's compile to make sure we haven't introduced any compilation error so far. 4:27
You need to sign up for Treehouse in order to download course files.Sign up