1 00:00:00,660 --> 00:00:03,910 On the surface abstract base classes appear to be more 2 00:00:03,910 --> 00:00:05,870 flexible than interfaces. 3 00:00:05,870 --> 00:00:08,050 They allow us to declare both public and 4 00:00:08,050 --> 00:00:12,980 protected members, that some classes must have and they can also contain code. 5 00:00:14,010 --> 00:00:18,550 Interfaces on the other hand can only declare the public members that subclasses 6 00:00:18,550 --> 00:00:19,930 must have and that's it. 7 00:00:20,960 --> 00:00:27,340 It's easy to see why we want to use abstract base classes, but why interfaces? 8 00:00:27,340 --> 00:00:31,350 Let's explore why interfaces are so useful in C#. 9 00:00:31,350 --> 00:00:34,785 You may think that interfaces are more restrictive versions of 10 00:00:34,785 --> 00:00:39,425 abstract base classes, but in fact, they serve different purposes. 11 00:00:39,425 --> 00:00:44,417 Abstract base classes are used to contain code that should be shared by the concrete 12 00:00:44,417 --> 00:00:46,216 subclass implementations. 13 00:00:46,216 --> 00:00:50,814 The implementations of all the non abstract members of the invader class 14 00:00:50,814 --> 00:00:53,008 are inherited by its subclasses. 15 00:00:53,008 --> 00:00:57,000 Each subclass doesn't have to create their own implementation. 16 00:00:57,000 --> 00:01:00,760 This is an advantage to use in an abstract base class. 17 00:01:00,760 --> 00:01:06,460 On the other hand C# only allows classes to inherit directly from one other class. 18 00:01:06,460 --> 00:01:10,780 Being able to inherit directly from multiple base classes is known as multiple 19 00:01:10,780 --> 00:01:12,270 inheritance. 20 00:01:12,270 --> 00:01:17,210 Multiple inheritance can cause a lot of problems so it isn't allowed in C#. 21 00:01:17,210 --> 00:01:22,030 Think of the case when two base classes both have methods that look identical. 22 00:01:22,030 --> 00:01:25,980 Which method implementation should be inherited in the subclass? 23 00:01:25,980 --> 00:01:31,735 Instead of multiple inheritance C# allows a class to Implement multiple interfaces. 24 00:01:31,735 --> 00:01:35,445 A class can implement as many interfaces as needed. 25 00:01:35,445 --> 00:01:38,525 An interface does nothing about the members' implementation. 26 00:01:38,525 --> 00:01:42,415 So, it doesn't matter if two interfaces both have the same member with 27 00:01:42,415 --> 00:01:43,415 the same signature. 28 00:01:44,465 --> 00:01:47,855 The class only needs to provide one implementation of the member 29 00:01:47,855 --> 00:01:49,030 to satisfy the interface. 30 00:01:49,030 --> 00:01:53,460 An interface can also inherit other interfaces. 31 00:01:53,460 --> 00:01:57,550 Let's break up the Iinvader interface into a couple different interfaces. 32 00:01:59,010 --> 00:02:02,795 We can break the location property out Into another interface or just cut it from 33 00:02:02,795 --> 00:02:08,901 here, Go up here and we'll create a new interface called IMappable. 34 00:02:10,584 --> 00:02:12,360 We'll move the property here. 35 00:02:12,360 --> 00:02:16,290 So now any class that implements the IMappable interface 36 00:02:16,290 --> 00:02:21,180 must have a public map location property that provides at the very least a getter. 37 00:02:21,180 --> 00:02:24,000 Just like how classes can implement multiple interfaces, 38 00:02:24,000 --> 00:02:27,850 interfaces can also inherit from multiple interfaces. 39 00:02:27,850 --> 00:02:31,309 Let's create an interface named IMovable. 40 00:02:31,309 --> 00:02:36,490 So say, interface I Move able. 41 00:02:36,490 --> 00:02:38,789 This one will require a single method named, 42 00:02:38,789 --> 00:02:41,400 Move which will take from the IInvader interface. 43 00:02:45,940 --> 00:02:47,420 There we go. 44 00:02:47,420 --> 00:02:51,520 To inherit from multiple interfaces, we simply list them after the colon and 45 00:02:51,520 --> 00:02:53,360 separate them with commas. 46 00:02:53,360 --> 00:02:59,160 So we'll have the IInvader interface inherit the IMappable and 47 00:02:59,160 --> 00:03:01,480 the IMovable interface. 48 00:03:01,480 --> 00:03:05,126 This is the same way that a class can implement multiple interfaces. 49 00:03:05,126 --> 00:03:07,740 We just list them here after the colon, 50 00:03:07,740 --> 00:03:10,910 just like we do when we inherit from another class. 51 00:03:10,910 --> 00:03:15,511 Now anything that implements the IInvader interface must also implement 52 00:03:15,511 --> 00:03:18,114 the IMappable and IMovable interfaces. 53 00:03:18,114 --> 00:03:23,158 That means in addition to having to have a HasScored, Health, IsNeutralized, 54 00:03:23,158 --> 00:03:29,090 IsActive and DecreaseHealth members, they also have to have Move and Location. 55 00:03:29,090 --> 00:03:33,426 The usefulness of interfaces isn't obvious right away, but they're important in good 56 00:03:33,426 --> 00:03:37,680 object oriented design and they make extending and maintaining code easier. 57 00:03:37,680 --> 00:03:40,400 The point of an interface is to only expose what is 58 00:03:40,400 --> 00:03:43,980 absolutely needed by the code that is using the class. 59 00:03:43,980 --> 00:03:47,000 This gives us the greatest flexibility when writing classes 60 00:03:47,000 --> 00:03:48,530 the implement the interface. 61 00:03:48,530 --> 00:03:50,880 We only need to match the interface. 62 00:03:50,880 --> 00:03:54,640 Say for example that we had a method that only needed to call the Move method 63 00:03:54,640 --> 00:03:56,410 on one of its parameters. 64 00:03:56,410 --> 00:04:00,690 We could code it so that it only expected a parameter of the type IMoveable. 65 00:04:00,690 --> 00:04:04,221 We could past that method any object of type IMovable, 66 00:04:04,221 --> 00:04:07,307 IInvader is a type IMovable and so is IInvader. 67 00:04:07,307 --> 00:04:10,860 And by extension so are all of the other invader subclasses. 68 00:04:10,860 --> 00:04:11,365 In fact, 69 00:04:11,365 --> 00:04:15,678 we could even pass the method a class that had nothing to do with being an invader. 70 00:04:15,678 --> 00:04:20,435 Say for example, we had a class named Bird that implemented the IMovable interface, 71 00:04:20,435 --> 00:04:23,886 we could also pass it to the method that expected in IMovable. 72 00:04:23,886 --> 00:04:26,146 Even though bird is nothing like an invader, 73 00:04:26,146 --> 00:04:30,810 they both have a Move method because they implement the IMovable interface. 74 00:04:30,810 --> 00:04:34,190 Can you see how using interfaces in our code gives us the most freedom 75 00:04:34,190 --> 00:04:36,430 in how we wire up our objects? 76 00:04:36,430 --> 00:04:39,550 Combining new interface with an abstract base class is a common pattern. 77 00:04:39,550 --> 00:04:43,120 It allows us to have the flexibility of an interface and 78 00:04:43,120 --> 00:04:47,230 also provides a mechanism to share code among subclasses. 79 00:04:47,230 --> 00:04:48,380 In the next video, 80 00:04:48,380 --> 00:04:53,160 we'll see how we can leverage the power of the IInvader interface to create a new 81 00:04:53,160 --> 00:04:56,790 type of invader that doesn't inherit from the invader base class at all