Interfaces4:27 with Jeremy McLain
Interfaces only say what the public methods and properties of a class should be.
We can take abstraction one step further. 0:00 What if we had an abstract base class that had no private or 0:04 protected members, and all of the public members were abstract. 0:08 This theoretical abstract base class would, in essence, 0:12 only define the public interface for concrete classes that inherit from it. 0:17 This is type abstraction in its purest form. 0:22 It turns out that C# has a special language feature 0:25 that allows us to do this. 0:28 It's aptly named an interface, and it's a subtly powerful feature of the language. 0:30 We'll begin to see why it's so useful over the next couple of videos. 0:35 We create interfaces very much like we create classes. 0:41 Typically, interfaces are defined in their own file. 0:45 So let's create a new file and name it IInvader. 0:47 Notice that we just took the name Invader and added a capital I in front of it. 0:51 Now in the RreehouseDdefense namespace, we'll create a new interface by 0:56 typing interface, and then the name of our interface, which is IInvader. 1:01 An interface defines what public members a class should have, so 1:07 we need to see what public members and invader should have. 1:10 Let's take a look at the Invader class to get an idea. 1:13 Essentially, what we want is all the public members of the class. 1:16 So let's just copy them from the Invader base class and 1:20 paste them in the interface. 1:23 So here we've got public location, this is all public, all the way down here. 1:25 Copy that and paste it here. 1:31 Now we need to clean them up a bit. 1:34 Interfaces don't have constructors, so we'll need to delete the constructor. 1:36 Just like abstract methods of an abstract base class, 1:42 methods in an interface don't have implementation. 1:45 So we need to delete the implementations from the move and decrease health methods. 1:48 Properties also don't have implementations, but 1:53 they still need to state whether the getter or setter is part of the interface. 1:56 The location property is a computed property, so it only needs a getter. 2:00 We write it like so. 2:04 HasScored, IsNeutralized and IsActive also just have getters. 2:09 Finally, interfaces only define public members. 2:17 So we don't need to have the public access modifier here. 2:20 The health property has a protected setter. 2:26 We can remove that from the interface as well. 2:28 I should say that this doesn't make these read only properties. 2:33 We can still set them when we go to implement this interface. 2:36 We'll see what that looks like in just a bit. 2:39 We also don't need to have the virtual or abstract modifiers here. 2:41 You might be interested to know that many IDEs such as Microsoft Visual Studio can 2:47 generate this interface code for you. 2:51 Using one of these interactive development environments, you can create an interface 2:54 from any existing class with just a few keystrokes or mouse clicks. 2:58 Let's take a look what we have here now. 3:01 We've boiled the Invader type down to the bare minimum interface 3:04 that other classes use to interact with it. 3:07 The interface defines what the properties and methods are, and 3:09 what their parameter and return types are. 3:13 It's as simple as that. 3:15 Now we can type :IInvader here in the Invader class declaration. 3:17 This isn't saying that the Invader class inherits from IInvader. 3:22 This is saying that the Invader implements the IInvader interface. 3:26 Interfaces behave very similar to types, and they can be used in place of classes. 3:30 We can replace all the places we're using the base Invader type 3:36 in the level class with IInvader. 3:39 So we'll change this to IInvader, and right here, this to IInvader, 3:41 and right here. 3:47 We'll also need to replace where Invader's being used in 3:52 Tower's FireOnInvaders method. 3:54 So right here and right here. 3:56 All right, that looks good. 4:03 Let's compile to make sure we haven't broken anything. 4:04 No compilation errors. 4:08 We haven't actually changed anything about how the game is played, 4:10 we've only done some refactoring. 4:13 The game now only expects the IInvader interface instead of the actual class. 4:16 In the next video, we'll discuss a bit more about 4:21 why we want to use an interface instead of a base class. 4:23
You need to sign up for Treehouse in order to download course files.Sign up