1 00:00:11,770 --> 00:00:13,540 Hi, everybody, and welcome. 2 00:00:13,840 --> 00:00:20,500 I'm super excited to talk about inheritance in this picture, but before proceeding to explaining it, 3 00:00:20,620 --> 00:00:24,010 let's go over the topics that we have already covered. 4 00:00:24,040 --> 00:00:30,280 OK, so we have already covered lots of topics in this course, and we've been through a lot of difficulties 5 00:00:30,280 --> 00:00:33,220 and lots of explanations and logics and assignments. 6 00:00:33,790 --> 00:00:42,220 So we've been through variables, collections, if statements, loop's methods, classes and objects. 7 00:00:42,550 --> 00:00:49,660 And you have seen in the previous lectures that all of these topics come integrated in object oriented 8 00:00:49,660 --> 00:00:55,750 programming in Python and you would need to dominate all of them in order to use OPIS. 9 00:00:57,040 --> 00:01:03,910 I'm sure that it wasn't easy for you to understand all the topics and to practice a lot and to loosen 10 00:01:03,910 --> 00:01:05,470 long hours of lectures. 11 00:01:05,470 --> 00:01:11,310 But I'm sure if you did now, you can surely say that you are professionals in Python programming. 12 00:01:12,460 --> 00:01:17,680 We're going to dive deeper into object oriented programming in this lecture and we're going to discuss 13 00:01:17,680 --> 00:01:21,370 a sub topic of OAP, which is called Inheritance. 14 00:01:21,640 --> 00:01:27,750 Inheritance is basically a behavior which is inside OPIS. 15 00:01:27,910 --> 00:01:34,480 It can be used not only in Python but also in other OPIS enhanced languages such as Java. 16 00:01:36,340 --> 00:01:40,530 Basically, inheritance is allowed. 17 00:01:40,610 --> 00:01:45,490 It allows us to have a parent child relationship between classes. 18 00:01:45,970 --> 00:01:53,890 We're going to create two classes and one of them would be defined as the parent and the other one would 19 00:01:53,890 --> 00:01:55,420 be defined as the child. 20 00:01:56,770 --> 00:02:00,490 In all cases, in computers, not only programmers. 21 00:02:00,680 --> 00:02:08,020 In programming, a parent is something that gives something to the child. 22 00:02:08,020 --> 00:02:09,310 It's above the child. 23 00:02:09,880 --> 00:02:18,220 And also in this case, that relationship that we're going to have between these two classes that we're 24 00:02:18,220 --> 00:02:19,940 going to create is the following. 25 00:02:20,650 --> 00:02:25,900 But first, let's talk about real life definition before we dive into coding. 26 00:02:27,440 --> 00:02:30,820 Let's assume that we have a broad. 27 00:02:32,000 --> 00:02:39,260 Category in all war in our world, this, we can call it a vehicle, OK, a vehicle is not something 28 00:02:39,260 --> 00:02:40,220 specific. 29 00:02:40,700 --> 00:02:45,020 A vehicle can be basically several stuff, several things. 30 00:02:45,020 --> 00:02:47,320 It can be a private car, a truck, a bus. 31 00:02:47,370 --> 00:02:52,360 OK, so if a vehicle is like a broad name for lots of things. 32 00:02:53,390 --> 00:02:58,460 So in our lecture, we're going to create a class with the name vehicle. 33 00:02:59,280 --> 00:03:05,300 OK, and you can already understand that vehicle would be the parent class. 34 00:03:05,330 --> 00:03:10,610 OK, so we're going to have two classes in the same Python file, first one at the top. 35 00:03:11,300 --> 00:03:12,620 This is going to be a vehicle. 36 00:03:12,850 --> 00:03:13,270 Right? 37 00:03:13,920 --> 00:03:20,120 And then beneath it, we're going to create a separate class, which will be a child class, in our 38 00:03:20,120 --> 00:03:21,640 case, private car. 39 00:03:22,910 --> 00:03:24,740 So we're going to have these two classes. 40 00:03:24,740 --> 00:03:25,130 Right. 41 00:03:25,620 --> 00:03:28,010 And why do we have in common between them? 42 00:03:28,490 --> 00:03:33,290 OK, so in vehicle, we're going to defined two things. 43 00:03:34,280 --> 00:03:41,330 But before talking about these two things, let's talk about common ground between all vehicles. 44 00:03:41,840 --> 00:03:52,880 Can you agree with me that all vehicles have wheels, doors, steering wheel, fuel tank and other items 45 00:03:53,000 --> 00:03:53,570 as well? 46 00:03:53,610 --> 00:03:54,010 Right. 47 00:03:54,440 --> 00:04:04,310 So we're going to define one method and one property in the vehicle class, and then we're going to 48 00:04:04,310 --> 00:04:09,680 use an instance of an object of a child class. 49 00:04:10,460 --> 00:04:16,470 And this child class is going to use this method and a property that we declared in the vehicle. 50 00:04:16,530 --> 00:04:21,680 OK, so the property in the method would be declared here. 51 00:04:22,840 --> 00:04:28,910 But we're going to create a separate class private car and using instance of this private car. 52 00:04:28,930 --> 00:04:29,350 Are you going? 53 00:04:29,560 --> 00:04:35,680 We're going to use this method in property which we created inside the vehicle, which is the parent. 54 00:04:35,900 --> 00:04:38,190 So you have an certain inheritance here. 55 00:04:38,560 --> 00:04:46,000 We're using property and methods inside the vehicle, but we're using a private car to initiate them. 56 00:04:46,030 --> 00:04:56,560 OK, so basically all child classes would absorb all the methods and properties of the parent class. 57 00:04:57,010 --> 00:04:57,490 OK. 58 00:04:58,560 --> 00:05:02,560 Hold it for me for one second of this sentence, OK? 59 00:05:02,790 --> 00:05:08,760 Child classes would absorb the methods and properties of the parent class and this is what we're going 60 00:05:08,760 --> 00:05:09,510 to do now. 61 00:05:09,540 --> 00:05:18,900 OK, so before talking how to do it, we're just going to see this example right here. 62 00:05:19,050 --> 00:05:19,500 Right. 63 00:05:20,610 --> 00:05:27,330 And let's go deeply and talk about all the details we see here, as always, making you like a monstrous 64 00:05:27,330 --> 00:05:28,440 example like this. 65 00:05:28,710 --> 00:05:32,700 But then once we break it down into small details, it's not so scary. 66 00:05:32,700 --> 00:05:33,090 Right. 67 00:05:33,870 --> 00:05:35,440 So let's start from the beginning. 68 00:05:35,460 --> 00:05:37,780 Let's start from the outer structure. 69 00:05:37,800 --> 00:05:43,500 OK, so what you see here is going to appear in one python file, right? 70 00:05:44,160 --> 00:05:48,300 In one python module you see here, class vehicle. 71 00:05:48,990 --> 00:05:53,010 And then you have the Init method and you have engine status method. 72 00:05:53,310 --> 00:05:55,450 Already familiar with this box. 73 00:05:55,460 --> 00:05:55,760 Right. 74 00:05:55,770 --> 00:05:58,650 Is the same thing as we covered lots of times. 75 00:06:00,390 --> 00:06:03,780 And here in the bottom part you can see an additional class. 76 00:06:04,170 --> 00:06:06,840 The name is Class Private Car. 77 00:06:07,110 --> 00:06:07,500 Right. 78 00:06:07,890 --> 00:06:12,030 And inside here you can also see def in it and that's it. 79 00:06:12,370 --> 00:06:14,040 OK, that's in it and that's it. 80 00:06:14,580 --> 00:06:17,100 We're going to discuss these parts here in a second. 81 00:06:17,110 --> 00:06:21,900 OK, but in overall, you can see here a class init method. 82 00:06:22,170 --> 00:06:29,640 And down here at the bottom you can see instance of an object that belongs to private car class. 83 00:06:31,070 --> 00:06:36,930 And you can see here, initiation or execution off engine status method. 84 00:06:37,010 --> 00:06:44,480 OK, so you have class parents, you have additional class child, you have instance of an object, 85 00:06:44,960 --> 00:06:48,260 an execution of a method. 86 00:06:48,530 --> 00:06:50,270 Now, let's go into details. 87 00:06:50,570 --> 00:06:55,520 OK, so up on here, you can see class private car. 88 00:06:55,820 --> 00:07:00,110 And inside these round brackets, you can see I mentioned here the war. 89 00:07:00,120 --> 00:07:03,740 The vehicle vehicle is the name of this class right here. 90 00:07:04,400 --> 00:07:04,760 Right. 91 00:07:07,050 --> 00:07:13,520 Going by the notes here at the right, you can see inherits all methods of vehicle class. 92 00:07:14,700 --> 00:07:21,960 So once I'm having this class right here and I'm mentioning a name of a different class, basically 93 00:07:21,960 --> 00:07:24,150 mentioning the name of the parent class. 94 00:07:25,540 --> 00:07:34,510 Then once I'm creating an instance of an object which belongs to the child private car, I can use the 95 00:07:34,510 --> 00:07:36,370 engine status method. 96 00:07:37,370 --> 00:07:38,720 Which is up in here. 97 00:07:40,720 --> 00:07:48,790 By this instance, so without this vehicle words right here, this is not possible because engine status 98 00:07:48,790 --> 00:07:52,490 method belongs to this part, to this class right here. 99 00:07:52,720 --> 00:07:58,450 So how come I have an instance belonging to private car and I'm using the method that belongs to a different 100 00:07:58,450 --> 00:08:05,920 class simply by using the vehicle, which is the parent class inside these wrong brackets. 101 00:08:06,010 --> 00:08:08,840 So this is inheritance of methods. 102 00:08:09,280 --> 00:08:10,450 Simple as that. 103 00:08:12,430 --> 00:08:15,730 Not once we covered the inheritance of methods. 104 00:08:16,150 --> 00:08:18,470 Let's cover the inheritance of properties. 105 00:08:18,530 --> 00:08:23,410 OK, it's going to be a bit more complicated, but follow me closely and I'm sure that you will be just 106 00:08:23,410 --> 00:08:25,940 fine and we're going to understand it quickly. 107 00:08:26,640 --> 00:08:35,440 OK, so going inside this definite method of the child right here, you can see here, first of all, 108 00:08:35,440 --> 00:08:40,380 self that is engine running equals engine is engine running. 109 00:08:40,390 --> 00:08:41,410 This is very simple. 110 00:08:41,410 --> 00:08:46,500 We already familiar with the first car, with the first property right here. 111 00:08:47,890 --> 00:08:53,710 But then you can see here Super Brackett's that you need. 112 00:08:53,800 --> 00:08:55,640 And then is the engine running OK? 113 00:08:55,930 --> 00:08:58,680 This line right here is a bit weird. 114 00:08:58,690 --> 00:08:59,860 We haven't seen it before. 115 00:09:00,910 --> 00:09:09,060 And this basically means is that we're having this instance here, right? 116 00:09:09,520 --> 00:09:14,140 We passed a true boolean variable into it. 117 00:09:14,560 --> 00:09:15,910 You see the number one here. 118 00:09:18,170 --> 00:09:26,750 It's received up here inside the declaration, and then from here, it went into this property, which 119 00:09:26,750 --> 00:09:31,520 is number three, and also went to this number three as well. 120 00:09:32,920 --> 00:09:43,270 And this super Nenita phrase allows us to pass the variable from here up to the top to here and then 121 00:09:43,270 --> 00:09:47,050 inside here and then inside engine status. 122 00:09:49,730 --> 00:10:01,070 So by using this super unit phrase, once we execute engine status, OK, once we execute this method 123 00:10:02,360 --> 00:10:11,150 and remember, we're using instance of an object of private car, it goes up in here, but it asks for 124 00:10:11,150 --> 00:10:13,390 a variable self is engine running. 125 00:10:13,400 --> 00:10:15,170 Is it true or is it false? 126 00:10:15,200 --> 00:10:16,190 It asks us. 127 00:10:17,120 --> 00:10:22,100 But the variable was passed to private car to this part right here. 128 00:10:23,120 --> 00:10:27,650 And we need somehow from this part to pass it to this part to the parent. 129 00:10:29,200 --> 00:10:37,690 And this is possible only by super any trace, so by using the super elite phrase, once we execute 130 00:10:37,690 --> 00:10:46,030 a method that belongs to the parent, by using the super eenie phrase, we're also using the variable. 131 00:10:47,110 --> 00:10:47,760 Through. 132 00:10:49,230 --> 00:10:58,680 Which is pass from here to here to here, then up to the end of the parent and then goes inside the 133 00:10:58,680 --> 00:11:00,360 method once it's executed. 134 00:11:01,020 --> 00:11:01,530 All right. 135 00:11:04,080 --> 00:11:05,650 This is not very complicated. 136 00:11:05,670 --> 00:11:07,850 Let's go over it again, literally. 137 00:11:07,860 --> 00:11:08,270 All right. 138 00:11:09,660 --> 00:11:16,650 So first of all, private car class inherits method and a property from vehicle class. 139 00:11:16,680 --> 00:11:25,230 OK, we understand that private car, for instance, this for focus equals private car inherits one 140 00:11:25,230 --> 00:11:28,770 method and one property from vehicle, which is the parent. 141 00:11:29,280 --> 00:11:32,970 Private car is the child while the vehicle is the parent. 142 00:11:34,110 --> 00:11:39,810 By mentioning vehicle in the declaration of a private car class, we achieve that. 143 00:11:39,810 --> 00:11:47,760 All methods from vehicle can be used by instance of private car by writing vehicle here. 144 00:11:49,700 --> 00:11:57,950 We can use Ingeus engine status method, which belongs to the top class, but by riding the vehicle 145 00:11:58,190 --> 00:12:04,610 keywords here, we can use all vehicles methods with this instance, which belongs to private car. 146 00:12:06,920 --> 00:12:09,030 This regards to methods. 147 00:12:09,290 --> 00:12:12,530 Now let's talk about the parameters, their property. 148 00:12:14,410 --> 00:12:20,470 In the parent class, we use a variable that is called is engine running, OK, you can see is engine 149 00:12:20,470 --> 00:12:22,360 running here, here and then inside. 150 00:12:22,360 --> 00:12:24,400 The method here is engine running. 151 00:12:27,620 --> 00:12:35,140 All right, in order to pass the property from private car class, we need we use a super unit phrase, 152 00:12:35,180 --> 00:12:37,620 OK, so we have this method right here. 153 00:12:37,670 --> 00:12:41,960 This method uses engine running, but it belongs to vehicle class. 154 00:12:42,920 --> 00:12:45,470 Here we have an instance of private car. 155 00:12:45,950 --> 00:12:52,100 So we insert a here variable, but we need somehow from this in here to pass it to the parent. 156 00:12:52,670 --> 00:12:56,000 And we're we're achieving this by super unit phrase. 157 00:12:56,000 --> 00:13:03,860 So we pass from here to here and into the method and it gets through here once we once we execute the 158 00:13:03,860 --> 00:13:04,730 engine status. 159 00:13:06,380 --> 00:13:13,300 OK, so in this example, I'm going to show you as well a live car example with the same code. 160 00:13:13,490 --> 00:13:13,960 All right. 161 00:13:14,750 --> 00:13:21,700 So you can see here the upper part class vehicle, the middle part class private car, and the last 162 00:13:21,710 --> 00:13:28,130 part, which is the instance of an object, then also the running of this metal right here. 163 00:13:29,540 --> 00:13:40,550 So first of all, I said that vehicle keyword is making is giving us the ability to use this instance 164 00:13:40,550 --> 00:13:42,940 of an object from here. 165 00:13:43,170 --> 00:13:47,570 It allows us to use engine status, which belongs to the upper class. 166 00:13:48,510 --> 00:13:49,550 So let's test it. 167 00:13:50,150 --> 00:13:52,520 Let me remove this keyword and see what happens. 168 00:13:53,780 --> 00:13:57,850 You see, once they remove this keyword immediately, you can see it back around here. 169 00:13:58,130 --> 00:14:05,330 And once they hover above it, it says unresolved attribute reference engine status for class, private 170 00:14:05,330 --> 00:14:05,640 car. 171 00:14:06,740 --> 00:14:12,100 It basically says to me, hey, do you have an instance of private car? 172 00:14:12,380 --> 00:14:17,120 Why the hell are you using a method that belongs to a different class? 173 00:14:17,420 --> 00:14:19,070 I'm not familiar with it. 174 00:14:19,370 --> 00:14:20,210 With that method. 175 00:14:20,390 --> 00:14:21,490 Not familiar with it. 176 00:14:21,740 --> 00:14:22,310 What is it? 177 00:14:22,310 --> 00:14:23,660 What is engine status? 178 00:14:23,840 --> 00:14:25,520 It's not part of private car. 179 00:14:26,620 --> 00:14:31,510 All right, but once I bring back the vehicle. 180 00:14:32,610 --> 00:14:33,120 OK. 181 00:14:34,140 --> 00:14:37,220 Once I bring back the vehicle into the wrong brackets, everything's fine. 182 00:14:39,100 --> 00:14:41,840 All right, so this is regarding the method. 183 00:14:42,160 --> 00:14:44,650 Now let's talk about the Parmeter, OK? 184 00:14:46,580 --> 00:14:51,080 You can see the super here, the freeze now let me try to remove it. 185 00:14:51,290 --> 00:14:52,960 OK, let's say we don't need it. 186 00:14:53,120 --> 00:14:56,230 And once we initiated this method, everything's fine. 187 00:14:56,240 --> 00:15:02,670 The true variable goes from here and goes automatically to the top into here, right into your status. 188 00:15:03,020 --> 00:15:04,700 Let me just try to remove this in it. 189 00:15:06,510 --> 00:15:12,270 And immediately you can see brownish background on the inside of the child here once hovering above 190 00:15:12,270 --> 00:15:12,510 it. 191 00:15:13,470 --> 00:15:14,540 Let's see what it says. 192 00:15:15,210 --> 00:15:16,950 Call to end it off. 193 00:15:16,950 --> 00:15:18,910 Superclass is missed, OK? 194 00:15:19,050 --> 00:15:20,010 He's telling me. 195 00:15:23,950 --> 00:15:28,510 He's telling me you're trying to get inheritance here, OK? 196 00:15:28,750 --> 00:15:34,600 You're trying to get inheritance here, but what about the super phrase, OK, I can't use inheritance 197 00:15:34,600 --> 00:15:35,700 without the super phrase. 198 00:15:37,030 --> 00:15:39,790 And once we have over here, everything's fine. 199 00:15:40,210 --> 00:15:46,600 OK, so this variable goes from here to here than to here and here. 200 00:15:46,780 --> 00:15:52,480 And then from this line, it goes up to here and then here. 201 00:15:53,080 --> 00:15:53,470 Right. 202 00:15:54,370 --> 00:15:56,980 And then when I run engine status. 203 00:15:58,050 --> 00:16:07,440 OK, this method I'm going to use is true, right, so it's already past up to here, so it's OK, it's 204 00:16:07,440 --> 00:16:08,490 going to get the value. 205 00:16:09,780 --> 00:16:10,800 Let's try to run it. 206 00:16:12,060 --> 00:16:13,920 OK, so I'll have it through here then. 207 00:16:13,970 --> 00:16:15,810 Engine is running then. 208 00:16:15,810 --> 00:16:22,050 If I put false here, I'm going to get engineers off. 209 00:16:22,800 --> 00:16:23,120 Right. 210 00:16:24,240 --> 00:16:27,600 And then once I'm going to put A, B, C here. 211 00:16:29,420 --> 00:16:31,930 So wrong value inserted. 212 00:16:32,160 --> 00:16:34,200 It's going to replace the deflection point. 213 00:16:34,760 --> 00:16:38,270 I think this covers perfectly the inheritance topic. 214 00:16:38,630 --> 00:16:43,490 You can always you can also find this block of code attached to the lecture. 215 00:16:43,640 --> 00:16:49,280 And I'm highly recommend you to copy it into a pie chart and to do some experiments, because my best 216 00:16:49,280 --> 00:16:54,770 learning experience was by making making experiments on other people's code. 217 00:16:55,370 --> 00:16:57,770 Thank you for listening and I will see you the next video.