Abstraction3:36 with Jeremy McLain
Abstraction isn't as abstract a concept as you might think.
[MUSIC] 0:00 Let's quickly recap what we know so far about object oriented programming. 0:05 With inheritance, we can create a subclass of a class. 0:11 In our project, we've created many subclasses of Invader. 0:15 We have FastInvader StrongInvader and ShieldedInvader. 0:18 Objects of type FastInvader StrongInvader and 0:23 shieldedInvader are also objects of type invader. 0:26 We also learned about interfaces. 0:30 The public methods and properties of a class are known as its interface. 0:32 This is how other classes interact with this class. 0:37 In the case of an Invader, other external classes can get the Invader's location. 0:40 See if it HasScored. 0:46 Check its health. 0:48 See if it has been neutralized. 0:49 Check if it's still active. 0:52 Ask it to move down the path and decrease its health. 0:54 All of these methods and properties are defined in the Invader class. 0:58 And because these other types of Invaders inherit from this base invader type, 1:02 they also have the same interface. 1:06 We could also add additional public methods to these subclasses, but 1:09 that would just be adding more members to the public interface for 1:13 that specific subclass. 1:16 So in object oriented programming, 1:18 we expose a public interface which other classes use to interact with the class. 1:20 We can override how each of these properties and 1:26 methods behave in subclasses using polymorphism. 1:28 That's the purpose of the virtual and override keywords in C#. 1:32 So long as these properties and 1:36 methods behave as expected, we can implement them however we want. 1:38 That's the idea behind encapsulation. 1:43 We don't usually need to know what other private members there are in a class or 1:46 exactly what the code for the public members looks like. 1:50 We only need to know that it takes our input and gives us the expected output. 1:54 That's why all the code that already works for Invader will also work for 1:59 FastInvader, StrongInvader and ShieldedInvader as well. 2:03 Thinking about software in terms of objects and their interfaces 2:08 greatly simplifies the way we think about architecting a software application. 2:12 It also makes it easy to extend an application. 2:17 Our new types of objects only need to be sure to implement the interface that 2:21 other parts of the software application expect to use. 2:25 Thinking about an object in terms of its public interface 2:29 is also known as abstraction. 2:32 Abstraction is the fourth and final core principle of object-oriented programming. 2:35 When architecting a software application, we don't need to think so 2:40 much about the concrete implementations of the class. 2:44 We only need to think about them in abstract terms. 2:47 For example, we don't need to know what the initial health of an Invader is or 2:51 even that there's a StrongInvader that has more initial health than other types 2:55 of invaders. 2:59 We just need to know that we can ask an Invader how much health it has. 3:00 When deciding about what an objects interface should be, 3:05 we need to think about it in abstract terms. 3:09 For example, what is the bare minimum set of methods and 3:12 properties that makes an invader, an invader? 3:15 This gives us the most freedom when it comes time to create specific 3:19 types of invaders. 3:23 This all seems a bit abstract right now and 3:24 it's hard to understand what all this means without seeing some real examples. 3:27 Let's see what this means by refactoring our Invader class a bit. 3:31
You need to sign up for Treehouse in order to download course files.Sign up