Abstract Classes3:12 with Jeremy McLain
Abstract classes are classes that can't be instantiated - the can only be subclassed.
So far we have four types of invaders. 0:00 We have a basic invader which we just call invader. 0:03 We also have fast invader, strong invader, and shielded invader, 0:06 these last three types of invaders are subclasses of our basic invader. 0:11 This is sort of odd, it makes more sense for 0:16 our basic invader to just be another type of invader. 0:19 We can define a base class that defines what it means to be an invader, but 0:22 isn't actually a type of invader that can be created. 0:27 This is called an abstract base class. 0:30 We can create an abstract base class by adding the key word abstract here, 0:33 in front of the word, class. 0:38 Now if we try to compile or program, we'll get a compilation error. 0:40 It says it cannot create an instance of the abstract class or 0:47 interface TreehouseDefense.Invade. 0:51 By adding the keyword abstract to the class definition, 0:54 we've said that this class is no longer a concrete implementation of an invader. 0:57 It only provides an abstract definition of what it means to be an invader. 1:02 We can no longer instantiate an invader, 1:07 we can only subclass it to create concrete implementations of it. 1:09 Fast invader, strong invader and 1:14 shielded invader are concrete implementations of the invader type. 1:16 We still want the ability to create a basic invader, so 1:20 we'll need to define a concrete implementation of one. 1:24 Let's create a new file named BasicInvader, and 1:27 copy the definition from FastInvader into it. 1:30 We'll change where it says FastInvader here to BasicInvader. 1:40 And we'll delete this StepSize property for now. 1:48 Now we have a concrete implementation of our BasicInvader type again. 1:53 We've named it BasicInvader because the name Invader 1:58 is already being used by the abstract base class. 2:01 Oftentimes, developers will append the word 2:04 base to the end of the abstract base class name. 2:07 So in this case we could rename our class to InvaderBase, this is 2:10 just a matter of personal preference, so I'm going to leave it as Invader for now. 2:16 Over in Game.cs, we're still trying to instantiate an Invader. 2:20 This is what is causing our compilation error here, 2:26 we need to change where it says new Invader to new BasicInvader. 2:29 By adding the abstract keyword to the invader class and creating the basic 2:34 invader class, we haven't actually changed anything about the game. 2:38 This is just a refactoring of the code, we've refactored it to communicate 2:41 the purpose of the invader base class more explicitly. 2:45 By making the invader class abstract, 2:48 we're telling other programmers that our code for 2:50 this game deals with the BaseInvader type and they can create their own invaders, 2:53 so long as they subclass the BaseInvader and implement their own invader types. 2:58 Their new types of invaders will work in the program and 3:02 existing code for the game. 3:05 Now if we compile we have a working program again. 3:07
You need to sign up for Treehouse in order to download course files.Sign up