1 00:00:00,560 --> 00:00:01,280 Welcome back. 2 00:00:01,400 --> 00:00:10,040 And this video we are going to look into interfaces and interfaces are a feature which allow us to extend 3 00:00:10,040 --> 00:00:11,990 the functionality of classes. 4 00:00:12,290 --> 00:00:19,870 So, for example, our car class could extend the functionality of an interface that has certain features, 5 00:00:19,880 --> 00:00:23,430 and let's go ahead and just create such an interface. 6 00:00:23,450 --> 00:00:29,930 So if this interface prepared here, which is called drivable, it has one value, which is a double 7 00:00:29,930 --> 00:00:31,820 value called max speed. 8 00:00:32,240 --> 00:00:36,320 Then it has a function called drive of type string, so it's going to return a string. 9 00:00:36,560 --> 00:00:42,350 But there is no implementation of the function, so it just doesn't have a function. 10 00:00:42,350 --> 00:00:44,780 Body only has to head of the function. 11 00:00:45,350 --> 00:00:49,490 And then we have a brake function, which actually has a function body as well. 12 00:00:49,820 --> 00:00:56,360 So an interface may provide a default implementation of some or all of its properties and functions, 13 00:00:56,570 --> 00:00:57,690 but it doesn't have to. 14 00:00:58,520 --> 00:01:04,640 And if a property or function has a default implementation, the class that uses this interface may 15 00:01:04,640 --> 00:01:05,780 choose to override it. 16 00:01:06,470 --> 00:01:07,250 What does that mean? 17 00:01:07,850 --> 00:01:11,570 Well, let's say we have this interface drivable right in there. 18 00:01:11,570 --> 00:01:13,910 We have these functions and this property. 19 00:01:14,420 --> 00:01:23,750 Now, if our class car decides to extend the functionality of the drivable interface. 20 00:01:24,990 --> 00:01:33,180 Then it needs to implement the property that is created in the interface, but is not implemented. 21 00:01:34,390 --> 00:01:42,430 So here we need to go ahead and say, wow, max speeds of type double should be in there. 22 00:01:43,570 --> 00:01:48,350 And because of your overriding something, so we are overriding the next speed from here. 23 00:01:48,370 --> 00:01:56,800 We need you at the keyword override that by itself doesn't make our class happy yet, as you can see, 24 00:01:56,920 --> 00:01:59,920 it's still not happy, it's still complaining something is still missing. 25 00:02:00,160 --> 00:02:04,960 And that is because we haven't implemented a member called Drive. 26 00:02:05,440 --> 00:02:12,490 So here's this drive function, and because it doesn't have a function body, we need to create one 27 00:02:12,820 --> 00:02:16,330 in the class that extends the interface. 28 00:02:17,020 --> 00:02:19,660 So we need to go ahead and create a drive function. 29 00:02:19,960 --> 00:02:22,600 So you can see we are already creating a drive function. 30 00:02:22,600 --> 00:02:24,130 Why is it still complaining? 31 00:02:24,460 --> 00:02:27,220 While this drive function doesn't follow the same structure. 32 00:02:27,670 --> 00:02:32,020 So you can see the head of the function doesn't return a string. 33 00:02:32,410 --> 00:02:34,270 So let's go ahead and actually do that. 34 00:02:34,270 --> 00:02:37,000 So override fun drive. 35 00:02:37,750 --> 00:02:41,460 And once again, fun drive. 36 00:02:41,470 --> 00:02:45,970 And you can see it gives me some help here, the idea. 37 00:02:45,970 --> 00:02:48,730 But it requires me to implement something. 38 00:02:48,730 --> 00:02:55,180 So I need to add a return statement here so I can, of course, go ahead and return. 39 00:02:56,780 --> 00:03:00,830 Driving the interface drive. 40 00:03:02,000 --> 00:03:04,520 OK, so that's just going to be the text that I return. 41 00:03:05,300 --> 00:03:11,030 Alternatively, what you could have done is you could have done it in a shorter way, for example, 42 00:03:11,540 --> 00:03:12,260 just saying. 43 00:03:14,350 --> 00:03:17,380 That it's going to return whatever the string is. 44 00:03:19,470 --> 00:03:19,830 OK. 45 00:03:20,100 --> 00:03:21,390 So both of them are fine. 46 00:03:21,660 --> 00:03:24,090 I can see this version is also fine. 47 00:03:24,090 --> 00:03:26,160 It's just a shorter version of this here. 48 00:03:29,300 --> 00:03:34,760 All right, so now we have multiple drive functions, and that is already starting to become confusing, 49 00:03:34,760 --> 00:03:35,180 maybe. 50 00:03:35,420 --> 00:03:36,500 But no worries. 51 00:03:36,890 --> 00:03:44,660 The one drive that we have here is actually the drive that comes from our interface that we are extending, 52 00:03:45,380 --> 00:03:45,770 OK? 53 00:03:46,890 --> 00:03:47,280 So. 54 00:03:48,240 --> 00:03:49,830 You can see here this drive. 55 00:03:49,920 --> 00:03:57,480 We implemented it and suddenly our car is not complaining anymore, then going back to our other drive 56 00:03:57,480 --> 00:04:02,130 that we have created, it's an open function drive, which is, so to speak, new. 57 00:04:02,130 --> 00:04:06,690 So this is the lowest level in which it's defined and that is in our class car. 58 00:04:07,080 --> 00:04:15,780 So this is another drive which we, of course, can override from other classes that inherit our car. 59 00:04:17,010 --> 00:04:24,840 Now, if electric car wants to inherit from car, it also has to follow the structure of our. 60 00:04:25,820 --> 00:04:26,600 Interface here. 61 00:04:27,380 --> 00:04:29,030 So let's also add. 62 00:04:30,270 --> 00:04:31,670 A Max Meteor. 63 00:04:34,290 --> 00:04:36,450 And let's use it here as well. 64 00:04:40,220 --> 00:04:46,940 Now, of course, our function drive that we had in our electric car that we created doesn't work anymore 65 00:04:47,690 --> 00:04:50,030 because now this drive. 66 00:04:51,060 --> 00:04:56,790 Is trying to override, so it thinks that we are trying to override the drive function. 67 00:04:57,840 --> 00:04:59,700 Which comes from here. 68 00:05:00,390 --> 00:05:00,870 This one. 69 00:05:03,100 --> 00:05:06,340 OK, so we also need to follow the structure of drive. 70 00:05:07,310 --> 00:05:15,410 Now, let's add the over right keyword, but then we also need to return the string type. 71 00:05:16,280 --> 00:05:17,900 And of course, here return. 72 00:05:19,740 --> 00:05:26,550 And we can use the same structure that we had before, or at least the same string that we are returning 73 00:05:26,550 --> 00:05:26,760 here. 74 00:05:29,270 --> 00:05:30,620 So what is an interface? 75 00:05:31,250 --> 00:05:36,200 Well, an interface is essentially a contract that a class may choose to sign. 76 00:05:36,740 --> 00:05:43,010 If it does, the class is obliged to provide implementations of the properties and functions of the 77 00:05:43,010 --> 00:05:43,610 interface. 78 00:05:44,640 --> 00:05:54,030 OK, so we see that here because our class car is extending the interface drivable. 79 00:05:54,450 --> 00:05:58,170 It's now obliged to implement its. 80 00:05:59,130 --> 00:06:03,750 Variables and functions or its properties and functions. 81 00:06:04,940 --> 00:06:12,380 The properties we did that by using it in the primary constructor and to function well, we implemented 82 00:06:12,380 --> 00:06:13,640 it directly here. 83 00:06:16,780 --> 00:06:24,100 You can also see, because our break had already an implementation, we never had to use it, but we 84 00:06:24,100 --> 00:06:25,040 can of course use it. 85 00:06:25,060 --> 00:06:27,220 We can even use it and a subclass here. 86 00:06:27,230 --> 00:06:33,580 So in the subclass of our car, in the electric car, we can still go ahead and implement brake so it 87 00:06:33,580 --> 00:06:34,930 can see override brake. 88 00:06:35,290 --> 00:06:41,680 And you can see this super keyword and that is to call the brake function of the superclass. 89 00:06:42,280 --> 00:06:50,170 And because our superclass, which is our car, didn't implement the functionality for brake, it will 90 00:06:50,170 --> 00:06:52,030 use the one from the interface. 91 00:06:52,030 --> 00:06:53,850 So it will just use this brake here. 92 00:06:54,990 --> 00:07:03,180 So now, if I go ahead and call break, and by the way, now of course, we are missing things when 93 00:07:03,180 --> 00:07:05,680 creating those Audi A3 and so forth. 94 00:07:05,700 --> 00:07:07,200 And I think it was speed, right? 95 00:07:07,560 --> 00:07:17,190 So 200 kilometers, I'd say, is the top speed here and 240 is going to be the top speed of the electric 96 00:07:17,190 --> 00:07:19,560 car and the errors disappear. 97 00:07:19,980 --> 00:07:23,100 But now which driver would be called when we called this drive? 98 00:07:23,610 --> 00:07:28,730 Well, if we look at it, one requires a distance, the other one doesn't. 99 00:07:28,740 --> 00:07:30,360 So it's going to call this drive. 100 00:07:31,840 --> 00:07:36,580 And then this year is, of course, going to require the distance, so it's going to call. 101 00:07:37,580 --> 00:07:38,540 This drive here. 102 00:07:40,540 --> 00:07:44,140 All right, now, let's say we want to call the brake function here. 103 00:07:44,410 --> 00:07:45,640 All right, let's just test it. 104 00:07:46,090 --> 00:07:51,040 Let's go ahead and use Tesla as the brake. 105 00:07:52,260 --> 00:07:56,310 And let's use already a three break. 106 00:07:58,930 --> 00:08:00,340 And here, of course, it's a break. 107 00:08:02,190 --> 00:08:07,710 So I'm going to get rid of the other calls here just to keep it simple. 108 00:08:11,340 --> 00:08:13,020 And let's actually run it here. 109 00:08:16,380 --> 00:08:23,520 And we will see that it says that drivable is breaking in both cases, so the case for Tesla and Order 110 00:08:23,520 --> 00:08:30,600 three, even though while we're already a three judges of type car never actually implement that the 111 00:08:30,740 --> 00:08:31,440 brake function? 112 00:08:31,750 --> 00:08:36,960 OK, so has it because it automatically inherited from brake. 113 00:08:38,400 --> 00:08:38,940 Now. 114 00:08:40,039 --> 00:08:46,160 What if we implement something here, let's say print line break inside of. 115 00:08:47,590 --> 00:08:48,310 The electric. 116 00:08:52,630 --> 00:08:53,500 Let's test it again. 117 00:08:55,290 --> 00:09:00,990 So you can see the drivable is breaking, and that is our test that. 118 00:09:01,940 --> 00:09:08,780 And then it says break inside of electric car is also our Tesla, and then we have this drivable is 119 00:09:08,780 --> 00:09:10,880 breaking, that's our audio that breaks. 120 00:09:11,450 --> 00:09:12,890 So what is happening? 121 00:09:13,250 --> 00:09:16,250 Well, when using override fun brake. 122 00:09:16,580 --> 00:09:23,390 So Super Brake, what happens is that the super function brake, so whatever was implemented for brake 123 00:09:23,390 --> 00:09:29,300 in the interface or in the superclass will be used or will be executed. 124 00:09:29,570 --> 00:09:35,420 And then on top of that, our own code that we add afterwards will also be executed. 125 00:09:35,960 --> 00:09:40,910 OK, so that is what this super dart method name does. 126 00:09:43,430 --> 00:09:51,930 But away subclasses of a class that implement an interface in this case are electric car are also considered 127 00:09:51,930 --> 00:09:53,850 to be implementing the interface. 128 00:09:54,520 --> 00:09:59,640 OK, I said it in a certain way, but I've never said it specifically like that. 129 00:09:59,680 --> 00:10:08,040 OK, so just so you know, our electric car is also implementing the interface called. 130 00:10:09,550 --> 00:10:10,210 Drivable. 131 00:10:12,390 --> 00:10:17,850 Another site notes an interface can inherit from another interface. 132 00:10:18,090 --> 00:10:24,480 So, yeah, in case you want to create your own interfaces that inherit from other interfaces, you 133 00:10:24,480 --> 00:10:25,200 can do so. 134 00:10:26,880 --> 00:10:29,010 So why would you even use interfaces? 135 00:10:29,520 --> 00:10:36,690 Well, interfaces are super useful when you have certain functions in mind that you definitely want 136 00:10:36,690 --> 00:10:42,390 to be implemented later on and also already know the properties of of a class that you want to then 137 00:10:42,450 --> 00:10:46,340 have implemented, but you don't want to implement them directly. 138 00:10:46,350 --> 00:10:53,670 You just want to prepare everything and you don't want to, for example, create a specific body already 139 00:10:53,670 --> 00:10:54,450 for a function. 140 00:10:55,960 --> 00:10:56,440 All right. 141 00:10:56,800 --> 00:11:02,440 So this is the introduction to interfaces, and in the next video, we're going to look at abstract 142 00:11:02,440 --> 00:11:07,420 classes, which are similar in a certain way to interfaces. 143 00:11:07,660 --> 00:11:11,040 But they have some differences in the nuances as well. 144 00:11:11,050 --> 00:11:12,550 So let's have a look at those.