1 00:00:00,000 --> 00:00:09,089 [MUSIC] 2 00:00:09,089 --> 00:00:10,364 In the last course, 3 00:00:10,364 --> 00:00:15,830 we saw how inheritance let's us use existing classes to help build new ones. 4 00:00:15,830 --> 00:00:20,620 Unfortunately, there are a couple problems that can't be solved by inheritance. 5 00:00:20,620 --> 00:00:24,090 Which means it's time to talk about the birds and the bees. 6 00:00:25,130 --> 00:00:26,900 You see, birds and 7 00:00:26,900 --> 00:00:32,450 bees are both capable of flight, meaning they both have a fly method. 8 00:00:32,450 --> 00:00:35,620 However, since not all animals can fly, 9 00:00:35,620 --> 00:00:40,700 these two fly methods are totally separate, which isn't ideal. 10 00:00:40,700 --> 00:00:45,800 What if we needed to create an array containing only animals that can fly? 11 00:00:45,800 --> 00:00:48,970 Right now, it would be pretty hard to do. 12 00:00:48,970 --> 00:00:53,500 Even though birds and bees have different implementations of the fly method, 13 00:00:53,500 --> 00:00:58,400 we can still pull the fly method out into an abstract class like this. 14 00:00:59,480 --> 00:01:01,410 Then if we make the Bird and 15 00:01:01,410 --> 00:01:07,240 Bee classes extend Flyable as well as Animal, we'd be good to go. 16 00:01:07,240 --> 00:01:11,920 Except of course that Java doesn't allow for multiple inheritance. 17 00:01:11,920 --> 00:01:15,400 To see why that is, let's look at an example. 18 00:01:15,400 --> 00:01:19,200 Let's say our Animal class has a method called speak. 19 00:01:19,200 --> 00:01:23,660 And extended from Animal, we have two classes that override that method. 20 00:01:23,660 --> 00:01:29,780 The Human class, which says Hello, and the Horse class, which says Hay. 21 00:01:29,780 --> 00:01:31,360 So far so good. 22 00:01:31,360 --> 00:01:33,650 Now, let's say someone comes along and 23 00:01:33,650 --> 00:01:39,360 makes a Centaur class by extending from both the Human and Horse classes. 24 00:01:39,360 --> 00:01:42,250 But for some reason, they don't override the speak method. 25 00:01:43,300 --> 00:01:45,900 This is called the diamond problem. 26 00:01:45,900 --> 00:01:50,090 If we called the speak method on a Centaur object, it's not clear 27 00:01:50,090 --> 00:01:53,650 if that's supposed to be the Human speak method or the Horse speak method. 28 00:01:54,680 --> 00:01:57,440 There's several ways to solve the diamond problem. 29 00:01:57,440 --> 00:02:02,130 But for Java, the solution was to get rid of multiple inheritance. 30 00:02:02,130 --> 00:02:04,870 Instead, we have interfaces. 31 00:02:04,870 --> 00:02:07,770 You can think of an interface as pretty much 32 00:02:07,770 --> 00:02:11,430 just a more restricted abstract class. 33 00:02:11,430 --> 00:02:15,080 Let's go back to the birds and the bees to see what I mean. 34 00:02:15,080 --> 00:02:20,210 On the left, we have the code from before, where we used multiple inheritance. 35 00:02:20,210 --> 00:02:23,290 And on the right, we're using an interface. 36 00:02:23,290 --> 00:02:26,920 These two pieces of code are exactly the same. 37 00:02:26,920 --> 00:02:30,460 Let's take a closer at the differences to see how to use an interface. 38 00:02:31,560 --> 00:02:36,650 For starters, when creating an interface, you start with the interface keyword. 39 00:02:36,650 --> 00:02:41,280 Then, just like with the class, you add the name and then the brackets. 40 00:02:42,480 --> 00:02:46,320 Inside an interface, there's really only two things you can do. 41 00:02:46,320 --> 00:02:49,840 You can declare a constant, or you can declare an abstract method. 42 00:02:50,870 --> 00:02:56,180 Since we're limited to just constants and abstract methods inside an interface, we 43 00:02:56,180 --> 00:03:01,890 don't need to specify static final for our variables, or abstract for our functions. 44 00:03:01,890 --> 00:03:04,320 It will just be that way by default. 45 00:03:04,320 --> 00:03:06,070 Once we've created the interface, 46 00:03:06,070 --> 00:03:10,250 we associate it with a class by using the implements keyword. 47 00:03:10,250 --> 00:03:13,130 Then, since interface methods are abstract, 48 00:03:13,130 --> 00:03:17,090 we need to override each of those methods in the class. 49 00:03:17,090 --> 00:03:20,640 This is one of the big advantages of interfaces. 50 00:03:20,640 --> 00:03:23,720 Since they can't contain any method implementations, 51 00:03:23,720 --> 00:03:26,790 it's impossible to run into the diamond problem. 52 00:03:26,790 --> 00:03:32,170 Which means we should have no problem implementing from more than one interface. 53 00:03:32,170 --> 00:03:35,240 So if we wanted to make our Bird class Flyable and 54 00:03:35,240 --> 00:03:39,570 Singable, we'd just add a comma and then the Singable interface. 55 00:03:40,670 --> 00:03:43,200 Awesome, that covers the basics. 56 00:03:43,200 --> 00:03:44,780 Let's take a short break, and 57 00:03:44,780 --> 00:03:49,010 when we come back, we'll open up IntelliJ and get some practice with interfaces.