1 00:00:00,000 --> 00:00:03,052 Great, you have your class that is now complete. 2 00:00:03,064 --> 00:00:06,000 Now let's say you want to create a class for a 3 00:00:06,001 --> 00:00:08,855 robotic arm. This class is actually a robot 4 00:00:08,867 --> 00:00:12,000 but with more functionalities that are specific 5 00:00:12,001 --> 00:00:15,336 to robotic arm. So to create the robotic arm 6 00:00:15,348 --> 00:00:19,000 class you will need to rewrite most of the robot 7 00:00:19,001 --> 00:00:22,634 class code here. And there is a way to avoid doing 8 00:00:22,646 --> 00:00:26,000 that and directly create the robotic arm class 9 00:00:26,001 --> 00:00:33,000 on top of this robot class. This is called inheritance and let's see what this is and 10 00:00:33,001 --> 00:00:35,890 how to do that directly with a code example. 11 00:00:35,902 --> 00:00:39,000 So I'm going to create, I'm going to stay in my 12 00:00:39,001 --> 00:00:42,219 robot.py file here and I'm going to create 13 00:00:42,231 --> 00:00:46,000 another class. So I go back to the line with zero 14 00:00:46,001 --> 00:00:50,631 indentation class robotic arm. Okay and I'm going 15 00:00:50,643 --> 00:00:55,000 to do things a little bit different. I'm going 16 00:00:55,001 --> 00:00:58,637 to open parentheses and put robot like this. This 17 00:00:58,649 --> 00:01:02,000 means that the robotic arm class will inherit 18 00:01:02,001 --> 00:01:05,597 from the robot class. So basically it will inherit 19 00:01:05,609 --> 00:01:09,000 from all the attributes and all the methods. So 20 00:01:09,001 --> 00:01:12,452 basically if I just write this and then if I do 21 00:01:12,464 --> 00:01:16,000 pass which is basically the python keyword to do 22 00:01:16,001 --> 00:01:18,513 nothing. For the robotic arm I can create a robotic 23 00:01:18,525 --> 00:01:21,000 arm with a name, a version number, it's also going 24 00:01:21,001 --> 00:01:24,108 to have an internal temperature and I can call sayHi 25 00:01:24,120 --> 00:01:27,000 init hardware and print info and that's going to 26 00:01:27,001 --> 00:01:30,083 work. Quite convenient right? And we can also add 27 00:01:30,095 --> 00:01:33,000 more attributes and methods inside the robotic 28 00:01:33,001 --> 00:01:36,769 arm class which are this time specific to a 29 00:01:36,781 --> 00:01:41,000 robotic arm and maybe don't apply to any kind of 30 00:01:41,001 --> 00:01:44,489 robot. Okay so I'm going to here, what I'm going 31 00:01:44,501 --> 00:01:48,000 to do, I'm going to do def init. So I'm going to 32 00:01:48,001 --> 00:01:53,246 create a constructor and I'm going to do self name, 33 00:01:53,258 --> 00:01:58,000 version number and reach. So let's say that to 34 00:01:58,001 --> 00:02:01,561 initialize the robotic arm I need to give a name, 35 00:02:01,573 --> 00:02:05,000 a version number, so just like I did here and a 36 00:02:05,001 --> 00:02:08,862 reach in millimeters for example. So I have my 37 00:02:08,874 --> 00:02:13,000 constructor here with the parameters and now what 38 00:02:13,001 --> 00:02:16,989 I'm going to do in the constructor I'm going to 39 00:02:17,001 --> 00:02:21,000 call super open close parentheses dot init okay 40 00:02:21,001 --> 00:02:26,803 with name, version number and then I'm going to 41 00:02:26,815 --> 00:02:33,000 do self dot reach is equal to reach. So what did I 42 00:02:33,001 --> 00:02:36,595 do here? Well for any class basically you are going 43 00:02:36,607 --> 00:02:40,000 to create a constructor okay and here if a class 44 00:02:40,001 --> 00:02:42,989 inherits from another class what you can do inside 45 00:02:43,001 --> 00:02:46,000 a method is called a super function and this super 46 00:02:46,001 --> 00:02:50,535 function is simply going to call a method that is 47 00:02:50,547 --> 00:02:55,000 from the what we can call the mother class okay. 48 00:02:55,001 --> 00:02:57,957 So if I do super dot init I'm going to call the 49 00:02:57,969 --> 00:03:01,000 constructor from the robot class so I don't need 50 00:03:01,001 --> 00:03:04,528 to write this another time inside this method 51 00:03:04,540 --> 00:03:08,000 okay. So I call the constructor from robot I 52 00:03:08,001 --> 00:03:10,515 initialize with the name the version number that 53 00:03:10,527 --> 00:03:13,000 I pass here okay initialize the temperature and 54 00:03:13,001 --> 00:03:16,867 then I add self dot reach is equal to reach so I 55 00:03:16,879 --> 00:03:21,000 add another attribute this time specific to robotic 56 00:03:21,001 --> 00:03:24,378 arm okay. So that's it for the constructor now 57 00:03:24,390 --> 00:03:28,000 I'm going to add another method for example pick. 58 00:03:30,000 --> 00:03:36,554 object with uh so self and then x and y coordinate 59 00:03:36,566 --> 00:03:43,000 for example let's just use a print pick object on 60 00:03:43,001 --> 00:03:55,000 and then plus string x and then plus let's add a comma here. 61 00:03:56,000 --> 00:04:01,988 str y plus and then close the parentheses okay 62 00:04:02,000 --> 00:04:08,000 pick object on x and y and I'm going to do def 63 00:04:08,001 --> 00:04:13,855 place object self x and y okay and I'm going 64 00:04:13,867 --> 00:04:20,000 to do the same thing here but instead of pick. 65 00:04:21,000 --> 00:04:24,449 place so I can pick and place an object okay 66 00:04:24,461 --> 00:04:28,000 that's very simplified for the example again. 67 00:04:28,001 --> 00:04:30,685 So here with robotic arm we have all the 68 00:04:30,697 --> 00:04:34,000 attributes and all the methods of the robot class 69 00:04:34,001 --> 00:04:36,989 and we have some specific as you can see some 70 00:04:37,001 --> 00:04:40,000 specific attributes and specific methods okay 71 00:04:40,001 --> 00:04:43,558 which are specific to a robotic arm and maybe don't 72 00:04:43,570 --> 00:04:47,000 apply to a drone or a submarine or any other kind 73 00:04:47,001 --> 00:04:49,927 of robot. All right and now I can use that class 74 00:04:49,939 --> 00:04:53,000 so this is still a simple class but I can use that 75 00:04:53,001 --> 00:04:57,489 class in my OOP example what I can do is I can 76 00:04:57,501 --> 00:05:02,000 do from robot import robotic arm so let's keep 77 00:05:02,001 --> 00:05:06,834 my robot here let's remove my robot two and I'm 78 00:05:06,846 --> 00:05:12,000 going to create my robotic arm is equal to robotic 79 00:05:12,001 --> 00:05:16,032 arm and I need to give a name so let's call it 80 00:05:16,044 --> 00:05:20,000 Bob a version number let's put four and reach 81 00:05:20,001 --> 00:05:25,095 let's put 300 for example and then what I can do 82 00:05:25,107 --> 00:05:30,000 I can do so my robotic arm dot print info okay 83 00:05:30,001 --> 00:05:36,000 I don't have print info here but I'm going to use the print info from the robot class 84 00:05:36,001 --> 00:05:44,000 and I can use for example my robotic arm dot pick object with let's say 85 00:05:44,001 --> 00:05:53,000 three and four and my robotic arm dot place objects five and six let's see what it does 86 00:05:53,001 --> 00:05:57,647 and as you can see so we have first the text 87 00:05:57,659 --> 00:06:02,000 here the print info of my robot okay r2d2 88 00:06:02,001 --> 00:06:04,989 and then we have the print info for so hello my 89 00:06:05,001 --> 00:06:08,000 name is Bob ready to help so Bob is the robotic 90 00:06:08,001 --> 00:06:11,989 arm version number four okay here and then you can 91 00:06:12,001 --> 00:06:16,000 see pick object on three four place object on five 92 00:06:16,001 --> 00:06:19,087 six okay and that pick object is only available 93 00:06:19,099 --> 00:06:22,000 for the robotic arm not for the normal robot 94 00:06:22,001 --> 00:06:25,019 and then well so you have one level of inheritance 95 00:06:25,031 --> 00:06:28,000 now you can create another class if you want that 96 00:06:28,001 --> 00:06:31,681 inherits from the robotic arm okay you can 97 00:06:31,693 --> 00:06:36,000 create multiple levels of inheritance and here as 98 00:06:36,001 --> 00:06:38,989 different examples that you can inherit from 99 00:06:39,001 --> 00:06:42,000 robot let's say you have a class for a drone 100 00:06:42,001 --> 00:06:45,854 or a mobile base or a submarine etc each of 101 00:06:45,866 --> 00:06:50,000 those classes can inherit from the robot class 102 00:06:50,001 --> 00:06:53,489 all right so to recap that is how you can use the 103 00:06:53,501 --> 00:06:57,000 power of inheritance to create programming blocks 104 00:06:57,001 --> 00:07:03,000 on top of other programming blocks without reinventing the wheel every time.