Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Interfaces vs Abstract Classes4:57 with Jeremy McLain
Abstract classes appear to be more powerful than interfaces, but interfaces have a very specific and useful purpose.
On the surface abstract base classes appear to be more 0:00 flexible than interfaces. 0:03 They allow us to declare both public and 0:05 protected members, that some classes must have and they can also contain code. 0:08 Interfaces on the other hand can only declare the public members that subclasses 0:14 must have and that's it. 0:18 It's easy to see why we want to use abstract base classes, but why interfaces? 0:20 Let's explore why interfaces are so useful in C#. 0:27 You may think that interfaces are more restrictive versions of 0:31 abstract base classes, but in fact, they serve different purposes. 0:34 Abstract base classes are used to contain code that should be shared by the concrete 0:39 subclass implementations. 0:44 The implementations of all the non abstract members of the invader class 0:46 are inherited by its subclasses. 0:50 Each subclass doesn't have to create their own implementation. 0:53 This is an advantage to use in an abstract base class. 0:57 On the other hand C# only allows classes to inherit directly from one other class. 1:00 Being able to inherit directly from multiple base classes is known as multiple 1:06 inheritance. 1:10 Multiple inheritance can cause a lot of problems so it isn't allowed in C#. 1:12 Think of the case when two base classes both have methods that look identical. 1:17 Which method implementation should be inherited in the subclass? 1:22 Instead of multiple inheritance C# allows a class to Implement multiple interfaces. 1:25 A class can implement as many interfaces as needed. 1:31 An interface does nothing about the members' implementation. 1:35 So, it doesn't matter if two interfaces both have the same member with 1:38 the same signature. 1:42 The class only needs to provide one implementation of the member 1:44 to satisfy the interface. 1:47 An interface can also inherit other interfaces. 1:49 Let's break up the Iinvader interface into a couple different interfaces. 1:53 We can break the location property out Into another interface or just cut it from 1:59 here, Go up here and we'll create a new interface called IMappable. 2:02 We'll move the property here. 2:10 So now any class that implements the IMappable interface 2:12 must have a public map location property that provides at the very least a getter. 2:16 Just like how classes can implement multiple interfaces, 2:21 interfaces can also inherit from multiple interfaces. 2:24 Let's create an interface named IMovable. 2:27 So say, interface I Move able. 2:31 This one will require a single method named, 2:36 Move which will take from the IInvader interface. 2:38 There we go. 2:45 To inherit from multiple interfaces, we simply list them after the colon and 2:47 separate them with commas. 2:51 So we'll have the IInvader interface inherit the IMappable and 2:53 the IMovable interface. 2:59 This is the same way that a class can implement multiple interfaces. 3:01 We just list them here after the colon, 3:05 just like we do when we inherit from another class. 3:07 Now anything that implements the IInvader interface must also implement 3:10 the IMappable and IMovable interfaces. 3:15 That means in addition to having to have a HasScored, Health, IsNeutralized, 3:18 IsActive and DecreaseHealth members, they also have to have Move and Location. 3:23 The usefulness of interfaces isn't obvious right away, but they're important in good 3:29 object oriented design and they make extending and maintaining code easier. 3:33 The point of an interface is to only expose what is 3:37 absolutely needed by the code that is using the class. 3:40 This gives us the greatest flexibility when writing classes 3:43 the implement the interface. 3:47 We only need to match the interface. 3:48 Say for example that we had a method that only needed to call the Move method 3:50 on one of its parameters. 3:54 We could code it so that it only expected a parameter of the type IMoveable. 3:56 We could past that method any object of type IMovable, 4:00 IInvader is a type IMovable and so is IInvader. 4:04 And by extension so are all of the other invader subclasses. 4:07 In fact, 4:10 we could even pass the method a class that had nothing to do with being an invader. 4:11 Say for example, we had a class named Bird that implemented the IMovable interface, 4:15 we could also pass it to the method that expected in IMovable. 4:20 Even though bird is nothing like an invader, 4:23 they both have a Move method because they implement the IMovable interface. 4:26 Can you see how using interfaces in our code gives us the most freedom 4:30 in how we wire up our objects? 4:34 Combining new interface with an abstract base class is a common pattern. 4:36 It allows us to have the flexibility of an interface and 4:39 also provides a mechanism to share code among subclasses. 4:43 In the next video, 4:47 we'll see how we can leverage the power of the IInvader interface to create a new 4:48 type of invader that doesn't inherit from the invader base class at all 4:53
You need to sign up for Treehouse in order to download course files.Sign up