WEBVTT 1 00:00:00.000 --> 00:00:01.150 Hi, and welcome back. 2 00:00:01.150 --> 00:00:04.290 In this video, we're talking about Inheritance. 3 00:00:04.290 --> 00:00:06.690 And if this scares you because it looks a bit advanced, 4 00:00:06.690 --> 00:00:07.990 don't worry. 5 00:00:07.990 --> 00:00:10.160 Inheritance is pretty easy to grasp, 6 00:00:10.160 --> 00:00:11.810 at least on some level, 7 00:00:11.810 --> 00:00:13.950 when we're gonna be talking about more advanced stuff 8 00:00:13.950 --> 00:00:15.770 as we move on through the course. 9 00:00:15.770 --> 00:00:18.430 Imagine you've got this student class, 10 00:00:18.430 --> 00:00:20.610 something very similar to what we've defined before, 11 00:00:20.610 --> 00:00:23.880 it's got an init method that takes in a name and a school 12 00:00:23.880 --> 00:00:25.620 and assigns them to two properties 13 00:00:25.620 --> 00:00:27.870 and it also creates a marks property, 14 00:00:27.870 --> 00:00:29.143 which is an empty list. 15 00:00:30.390 --> 00:00:33.230 Then it also has an average function method. 16 00:00:33.230 --> 00:00:34.930 I always wanna call them functions, 17 00:00:34.930 --> 00:00:36.980 an average method that returns 18 00:00:36.980 --> 00:00:38.783 the average of self.marks. 19 00:00:40.440 --> 00:00:44.970 Now, imagine you want to add some more functionality 20 00:00:44.970 --> 00:00:48.350 to another class which may be a working student class. 21 00:00:48.350 --> 00:00:51.130 A working student is exactly the same as a student 22 00:00:51.130 --> 00:00:53.430 but it has a salary as well. 23 00:00:53.430 --> 00:00:54.263 Let's do that. 24 00:00:55.586 --> 00:00:56.970 (keyboard clacks) 25 00:00:56.970 --> 00:00:58.487 Here's our working student class 26 00:00:58.487 --> 00:01:00.393 and it has an init method too. 27 00:01:01.810 --> 00:01:04.553 Whereas it takes a name, a school and a salary. 28 00:01:08.070 --> 00:01:10.570 Now, same as above, would you rather have 29 00:01:10.570 --> 00:01:14.640 self.name equals name, self.school equals school, 30 00:01:14.640 --> 00:01:17.470 self.marks equals empty list, and now a new line, 31 00:01:17.470 --> 00:01:19.453 self.salary equals salary. 32 00:01:20.590 --> 00:01:23.630 Whenever you are writing stuff like this 33 00:01:24.700 --> 00:01:28.060 that is exact copy of something you have above, 34 00:01:28.060 --> 00:01:31.230 you must know that something is wrong. 35 00:01:31.230 --> 00:01:33.010 Something can be made better. 36 00:01:33.010 --> 00:01:36.350 Particularly in Python, where duplication 37 00:01:36.350 --> 00:01:39.640 is not really that common. 38 00:01:39.640 --> 00:01:40.990 There are always way to make your code, 39 00:01:40.990 --> 00:01:43.943 There are always ways to make your code less duplicate. 40 00:01:45.200 --> 00:01:47.790 We also have to define, of course, this average... 41 00:01:49.540 --> 00:01:53.080 method where it's gonna return everything as we know it 42 00:01:55.060 --> 00:01:55.893 like so. 43 00:01:57.130 --> 00:02:02.130 Now, if we create a working student and student, 44 00:02:02.130 --> 00:02:06.860 Rolf, that goes to MIT, and has a salary of 15.50, 45 00:02:06.860 --> 00:02:09.680 we can, of course, print out rolf.salary 46 00:02:11.000 --> 00:02:14.460 and that's gonna give us the salary property, 15.5. 47 00:02:14.460 --> 00:02:17.860 Notice how Python drops the last zero because it's a zero 48 00:02:17.860 --> 00:02:18.693 and that's fine. 49 00:02:19.560 --> 00:02:21.560 So again, nothing new is going on here, 50 00:02:21.560 --> 00:02:23.820 working student has created the new blank object, 51 00:02:23.820 --> 00:02:25.110 let's put it into self. 52 00:02:25.110 --> 00:02:28.500 Rolf MIT and 15.50 have gone into name, school, and salary 53 00:02:28.500 --> 00:02:31.860 respectively, then we've created four properties: 54 00:02:31.860 --> 00:02:34.660 self.name, self.school, self.marks and self.salary, 55 00:02:34.660 --> 00:02:37.840 and we've given it some values: name, school and salary, 56 00:02:37.840 --> 00:02:40.820 these are the parameters, and empty list for marks 57 00:02:40.820 --> 00:02:41.970 to be initialised with. 58 00:02:42.940 --> 00:02:44.400 If we wanted to add some marks, 59 00:02:44.400 --> 00:02:49.400 we would have to do rolf.marks.append(56), for example. 60 00:02:51.540 --> 00:02:54.340 And then, we could calculate the average of those marks. 61 00:02:55.570 --> 00:02:57.220 So you can see, there's a lot of jublication 62 00:02:57.220 --> 00:03:00.390 between our student, which has a name, school and marks 63 00:03:00.390 --> 00:03:03.090 and an average function, and our working student, 64 00:03:03.090 --> 00:03:04.530 which is exactly the same, 65 00:03:04.530 --> 00:03:06.780 but the only thing we've added is the salary. 66 00:03:09.550 --> 00:03:14.550 So, we could use Inheritance in order to remove jublication. 67 00:03:16.580 --> 00:03:17.860 Here's how it's gonna go. 68 00:03:17.860 --> 00:03:20.780 I'm going to reimplement the working student class 69 00:03:20.780 --> 00:03:23.300 using Inheritance to reduce duplication, 70 00:03:23.300 --> 00:03:25.950 and then we're gonna talk about what's going on. 71 00:03:25.950 --> 00:03:28.050 So, we've got our class working student 72 00:03:28.050 --> 00:03:32.170 and we want to make it extend the student class. 73 00:03:32.170 --> 00:03:34.700 What that's gonna mean is the working student's class 74 00:03:34.700 --> 00:03:38.130 is going to become an exact copy of student 75 00:03:38.130 --> 00:03:41.730 but we're going to be able to modify particular methods 76 00:03:41.730 --> 00:03:43.290 if we want. 77 00:03:43.290 --> 00:03:45.920 The way you do that is you add some brackets at the end 78 00:03:45.920 --> 00:03:48.920 and you type in Student. 79 00:03:48.920 --> 00:03:53.300 Now, working student is a class that is a child of student. 80 00:03:53.300 --> 00:03:55.153 It extends student. 81 00:03:57.130 --> 00:04:01.031 So, we can delete the average method. 82 00:04:01.031 --> 00:04:02.450 (instructor whistles) Completely gone, 83 00:04:02.450 --> 00:04:06.233 but working student still has an average method. 84 00:04:07.430 --> 00:04:08.650 Only it's not there, 85 00:04:08.650 --> 00:04:12.280 it is now directly inherited from student. 86 00:04:12.280 --> 00:04:14.390 Exactly the same, imagine it's a copy and paste. 87 00:04:14.390 --> 00:04:15.393 Nothing's changed. 88 00:04:16.780 --> 00:04:18.580 So, what about the init method then? 89 00:04:19.590 --> 00:04:21.620 We've got name and a school, which are just copies of them. 90 00:04:21.620 --> 00:04:25.063 Can't we just delete them and just keep the salary? 91 00:04:26.530 --> 00:04:29.510 This would be great, I'll be honest. 92 00:04:29.510 --> 00:04:32.900 It would be great if we could do this, but sadly, no 93 00:04:32.900 --> 00:04:35.800 because when working student is now called, 94 00:04:35.800 --> 00:04:37.860 the same thing happens as before. 95 00:04:37.860 --> 00:04:41.483 An empty, blank object is created given to self, 96 00:04:42.450 --> 00:04:46.300 and now salary is rolf. 97 00:04:46.300 --> 00:04:50.670 MIT and 15.50 don't have any matching arguments 98 00:04:50.670 --> 00:04:52.930 so we get an error, too many positional arguments 99 00:04:52.930 --> 00:04:54.163 for constructor call. 100 00:04:55.090 --> 00:04:57.623 So no, you cannot do that unfortunately. 101 00:04:58.710 --> 00:05:00.190 But what you can do 102 00:05:00.190 --> 00:05:03.360 is now that you have this empty object here, 103 00:05:03.360 --> 00:05:05.950 you can call the student's init method 104 00:05:05.950 --> 00:05:08.900 and make it go through this initialization 105 00:05:08.900 --> 00:05:12.020 of self.name, self.school and self.marks. 106 00:05:12.020 --> 00:05:15.380 And the way you do that is a mildly confusing way. 107 00:05:15.380 --> 00:05:16.363 You say super. 108 00:05:17.420 --> 00:05:21.763 Super is the parent class, in this case, student. 109 00:05:22.640 --> 00:05:25.130 And you can then, this is the only time 110 00:05:25.130 --> 00:05:28.540 when you are gonna call the init method manually. 111 00:05:28.540 --> 00:05:30.530 You're gonna say super, which is the parent class, 112 00:05:30.530 --> 00:05:33.990 student. dunder init, open and closed brackets, 113 00:05:33.990 --> 00:05:36.840 and in here, you're gonna put the name and the school. 114 00:05:36.840 --> 00:05:40.710 So what is happening then is you are just jumping over 115 00:05:40.710 --> 00:05:45.340 to this init method and self is stealing the current object 116 00:05:45.340 --> 00:05:46.173 that you're dealing with. 117 00:05:46.173 --> 00:05:48.603 That's rolf, there's working student. 118 00:05:50.160 --> 00:05:54.240 So now, here, you call the parent's classing it's method, 119 00:05:54.240 --> 00:05:57.160 that initialises name, school, and marks. 120 00:05:57.160 --> 00:05:59.923 Then, you can back down here, and you initialise salary. 121 00:06:00.770 --> 00:06:03.040 And this is a much more concise way 122 00:06:03.040 --> 00:06:05.010 of writing your working student class 123 00:06:05.010 --> 00:06:07.870 that is just an extension of the student class. 124 00:06:07.870 --> 00:06:10.173 It just adds a new property to it. 125 00:06:12.690 --> 00:06:15.250 Notice how you can print Rolf's salary, 126 00:06:15.250 --> 00:06:19.507 but you can also do rolf.marks, marks.append(57), 127 00:06:20.570 --> 00:06:22.337 rolf.marks.append(99), 128 00:06:24.590 --> 00:06:28.880 and you can print the average there. 129 00:06:28.880 --> 00:06:31.930 Then you can run it and 78.0 comes out, 130 00:06:31.930 --> 00:06:34.260 that's totally good because that means 131 00:06:34.260 --> 00:06:36.680 that our working student has this salary, 132 00:06:36.680 --> 00:06:41.500 this average function, method defined in here 133 00:06:41.500 --> 00:06:43.070 and that's all good. 134 00:06:43.070 --> 00:06:45.240 It's just inherited it from the student class 135 00:06:45.240 --> 00:06:47.000 as opposed to having to define it. 136 00:06:47.000 --> 00:06:50.023 That's the basis of inheritance. 137 00:06:51.450 --> 00:06:56.320 And the way to extend the class 138 00:06:56.320 --> 00:07:01.320 is now you can define something else like weekly salary. 139 00:07:06.241 --> 00:07:08.800 And this is gonna return self.salary times 37.5. 140 00:07:12.640 --> 00:07:16.500 Thus the normal working hours in the UK, 37.5 hours, 141 00:07:16.500 --> 00:07:19.130 so now, you can print rolf.weekly_salary, 142 00:07:20.130 --> 00:07:24.830 and that's gonna give you Rolf's salary times 37.5. 143 00:07:26.540 --> 00:07:29.730 Important if you create... 144 00:07:29.730 --> 00:07:31.980 I'm gonna add a few lines there 145 00:07:31.980 --> 00:07:32.900 so it's a bit easier to read, 146 00:07:32.900 --> 00:07:36.090 if you create Anna being a normal student 147 00:07:37.560 --> 00:07:41.170 at Oxford, notice how she doesn't take a salary 148 00:07:41.170 --> 00:07:43.210 because the student constructor is still up here, 149 00:07:43.210 --> 00:07:44.720 this class is still totally valid, 150 00:07:44.720 --> 00:07:46.920 it doesn't stop being useful, 151 00:07:46.920 --> 00:07:50.550 doesn't take a salary, doesn't have a salary property. 152 00:07:50.550 --> 00:07:53.880 So, now, if you do anna.weekly_salary, 153 00:07:56.110 --> 00:07:58.020 this is gonna give you an error 154 00:07:58.020 --> 00:08:00.210 and it's gonna say the student object 155 00:08:00.210 --> 00:08:01.950 has no attribute weekly salary. 156 00:08:01.950 --> 00:08:05.860 This function here is not defined for student. 157 00:08:05.860 --> 00:08:08.163 It's only defined in working student. 158 00:08:09.050 --> 00:08:10.970 This goes from top to bottom. 159 00:08:10.970 --> 00:08:14.220 Working student gets stuff from student 160 00:08:14.220 --> 00:08:15.380 but not the other way around. 161 00:08:15.380 --> 00:08:18.890 Student does not get anything from working student, okay? 162 00:08:18.890 --> 00:08:20.290 Important thing to remember. 163 00:08:21.680 --> 00:08:24.300 That's it for this video, now in the next video, 164 00:08:24.300 --> 00:08:28.300 we're going to look at the property decorator. 165 00:08:28.300 --> 00:08:30.680 We're not gonna talk about what decorators are, 166 00:08:30.680 --> 00:08:32.330 but what we're gonna talk about is the fact 167 00:08:32.330 --> 00:08:35.320 that we've got this weekly salary thing here 168 00:08:35.320 --> 00:08:37.410 and it's a function that takes no arguments 169 00:08:37.410 --> 00:08:38.453 other than self. 170 00:08:39.420 --> 00:08:43.040 So, there's some simplification we can do here. 171 00:08:43.040 --> 00:08:45.190 Let's talk about it in the very next video.