1 00:00:00,000 --> 00:00:05,096 [MUSIC] 2 00:00:05,096 --> 00:00:09,790 Let's quickly recap what we know so far about object oriented programming. 3 00:00:11,150 --> 00:00:15,260 With inheritance, we can create a subclass of a class. 4 00:00:15,260 --> 00:00:18,696 In our project, we've created many subclasses of Invader. 5 00:00:18,696 --> 00:00:22,210 We have FastInvader StrongInvader and ShieldedInvader. 6 00:00:23,280 --> 00:00:26,160 Objects of type FastInvader StrongInvader and 7 00:00:26,160 --> 00:00:30,270 shieldedInvader are also objects of type invader. 8 00:00:30,270 --> 00:00:32,431 We also learned about interfaces. 9 00:00:32,431 --> 00:00:37,280 The public methods and properties of a class are known as its interface. 10 00:00:37,280 --> 00:00:40,810 This is how other classes interact with this class. 11 00:00:40,810 --> 00:00:46,777 In the case of an Invader, other external classes can get the Invader's location. 12 00:00:46,777 --> 00:00:48,424 See if it HasScored. 13 00:00:48,424 --> 00:00:49,734 Check its health. 14 00:00:49,734 --> 00:00:52,200 See if it has been neutralized. 15 00:00:52,200 --> 00:00:54,415 Check if it's still active. 16 00:00:54,415 --> 00:00:57,460 Ask it to move down the path and decrease its health. 17 00:00:58,560 --> 00:01:02,127 All of these methods and properties are defined in the Invader class. 18 00:01:02,127 --> 00:01:06,564 And because these other types of Invaders inherit from this base invader type, 19 00:01:06,564 --> 00:01:09,360 they also have the same interface. 20 00:01:09,360 --> 00:01:13,490 We could also add additional public methods to these subclasses, but 21 00:01:13,490 --> 00:01:16,230 that would just be adding more members to the public interface for 22 00:01:16,230 --> 00:01:18,610 that specific subclass. 23 00:01:18,610 --> 00:01:20,610 So in object oriented programming, 24 00:01:20,610 --> 00:01:26,110 we expose a public interface which other classes use to interact with the class. 25 00:01:26,110 --> 00:01:28,350 We can override how each of these properties and 26 00:01:28,350 --> 00:01:32,210 methods behave in subclasses using polymorphism. 27 00:01:32,210 --> 00:01:35,776 That's the purpose of the virtual and override keywords in C#. 28 00:01:36,940 --> 00:01:38,420 So long as these properties and 29 00:01:38,420 --> 00:01:43,380 methods behave as expected, we can implement them however we want. 30 00:01:43,380 --> 00:01:46,230 That's the idea behind encapsulation. 31 00:01:46,230 --> 00:01:50,340 We don't usually need to know what other private members there are in a class or 32 00:01:50,340 --> 00:01:52,990 exactly what the code for the public members looks like. 33 00:01:54,040 --> 00:01:59,360 We only need to know that it takes our input and gives us the expected output. 34 00:01:59,360 --> 00:02:03,633 That's why all the code that already works for Invader will also work for 35 00:02:03,633 --> 00:02:08,230 FastInvader, StrongInvader and ShieldedInvader as well. 36 00:02:08,230 --> 00:02:12,570 Thinking about software in terms of objects and their interfaces 37 00:02:12,570 --> 00:02:17,690 greatly simplifies the way we think about architecting a software application. 38 00:02:17,690 --> 00:02:20,190 It also makes it easy to extend an application. 39 00:02:21,480 --> 00:02:25,690 Our new types of objects only need to be sure to implement the interface that 40 00:02:25,690 --> 00:02:28,480 other parts of the software application expect to use. 41 00:02:29,620 --> 00:02:32,750 Thinking about an object in terms of its public interface 42 00:02:32,750 --> 00:02:35,250 is also known as abstraction. 43 00:02:35,250 --> 00:02:40,491 Abstraction is the fourth and final core principle of object-oriented programming. 44 00:02:40,491 --> 00:02:44,125 When architecting a software application, we don't need to think so 45 00:02:44,125 --> 00:02:47,795 much about the concrete implementations of the class. 46 00:02:47,795 --> 00:02:51,066 We only need to think about them in abstract terms. 47 00:02:51,066 --> 00:02:55,421 For example, we don't need to know what the initial health of an Invader is or 48 00:02:55,421 --> 00:02:59,977 even that there's a StrongInvader that has more initial health than other types 49 00:02:59,977 --> 00:03:00,781 of invaders. 50 00:03:00,781 --> 00:03:04,865 We just need to know that we can ask an Invader how much health it has. 51 00:03:05,890 --> 00:03:09,260 When deciding about what an objects interface should be, 52 00:03:09,260 --> 00:03:12,000 we need to think about it in abstract terms. 53 00:03:12,000 --> 00:03:15,600 For example, what is the bare minimum set of methods and 54 00:03:15,600 --> 00:03:19,130 properties that makes an invader, an invader? 55 00:03:19,130 --> 00:03:23,040 This gives us the most freedom when it comes time to create specific 56 00:03:23,040 --> 00:03:23,900 types of invaders. 57 00:03:24,990 --> 00:03:27,279 This all seems a bit abstract right now and 58 00:03:27,279 --> 00:03:31,741 it's hard to understand what all this means without seeing some real examples. 59 00:03:31,741 --> 00:03:35,570 Let's see what this means by refactoring our Invader class a bit.