1 00:00:00,000 --> 00:00:02,670 Great, you have your class that is now complete. 2 00:00:02,940 --> 00:00:06,150 Now let's say you want to create a class for a 3 00:00:06,180 --> 00:00:09,780 robotic arm. This class is actually a Robot, but 4 00:00:09,810 --> 00:00:12,540 with more functionalities, okay that are specific 5 00:00:12,600 --> 00:00:15,540 to robotic arm. So to create the robotic arm 6 00:00:15,540 --> 00:00:19,320 class, you will need to rewrite most of the Robot 7 00:00:19,380 --> 00:00:22,740 class code here, and there is a way to avoid doing 8 00:00:22,740 --> 00:00:26,430 that and directly create the robotic arm class on 9 00:00:26,460 --> 00:00:30,300 top of this Robot class. This is called 10 00:00:30,360 --> 00:00:33,930 inheritance. And let's see what this is and how to 11 00:00:33,930 --> 00:00:37,230 do that directly with a code example. So I'm going 12 00:00:37,230 --> 00:00:40,680 to create, I'm going to stay in my robot.py file 13 00:00:40,680 --> 00:00:43,740 here, and I'm going to create another class. So I 14 00:00:43,740 --> 00:00:50,371 go back to the line with zero indentation, class RoboticArm. 15 00:00:50,371 --> 00:00:53,820 Okay, and I'm going to do things a little 16 00:00:53,820 --> 00:00:56,610 bit different, I'm going to open parentheses and 17 00:00:56,610 --> 00:01:00,983 put Robot like this. This means that the RoboticArm. 18 00:01:00,983 --> 00:01:04,290 class will inherit from the Robot class. So 19 00:01:04,290 --> 00:01:07,350 basically, it will inherit from all the attributes 20 00:01:07,440 --> 00:01:10,380 and all the methods. So basically, if I just write 21 00:01:10,380 --> 00:01:13,500 this, and then if I do pass, which is basically 22 00:01:13,500 --> 00:01:17,727 the Python keyword, to do nothing, for the RoboticArm 23 00:01:17,727 --> 00:01:19,680 I can create a RoboticArm with the name, 24 00:01:19,710 --> 00:01:22,020 version number, it's also going to have an 25 00:01:22,050 --> 00:01:25,660 internal temperature, and I can call say_hi, init_hardware, 26 00:01:25,660 --> 00:01:27,720 and print_info, and that's going to work. 27 00:01:27,960 --> 00:01:30,900 Quite convenient, right? And we can also add more 28 00:01:30,900 --> 00:01:33,750 attributes and methods inside the RoboticArm 29 00:01:33,780 --> 00:01:38,161 class, which are this time specific to a RoboticArm, 30 00:01:38,161 --> 00:01:41,400 and maybe don't apply to any kind of robots. 31 00:01:41,460 --> 00:01:44,310 Okay, so I'm going to here, what am I going to do, 32 00:01:44,310 --> 00:01:48,660 I'm going to def __init__. So I'm going to create a 33 00:01:48,660 --> 00:01:52,740 constructor, and I'm going to do self name, 34 00:01:53,580 --> 00:01:58,320 version_number, and reach. So let's say that to 35 00:01:58,320 --> 00:02:01,020 initialize the RoboticArm, I need to give a name, 36 00:02:01,050 --> 00:02:05,070 version_number, so just like I did here, and a 37 00:02:05,070 --> 00:02:09,690 reach in millimeters, for example. So I have my 38 00:02:09,720 --> 00:02:13,050 constructor here with the parameters, and now what I'm 39 00:02:13,050 --> 00:02:15,030 going to do, in the constructor I'm going to call 40 00:02:15,030 --> 00:02:21,360 super open, closed parenthesis .__init__, okay, with 41 00:02:21,360 --> 00:02:26,610 name, version_number, and then I'm going to do 42 00:02:26,610 --> 00:02:33,600 self.reach is equal to reach. So what did I do 43 00:02:33,600 --> 00:02:36,510 here? Well, for any class, basically, you're going 44 00:02:36,510 --> 00:02:39,630 to create a constructor, okay. And here, if a 45 00:02:39,630 --> 00:02:43,020 class inherits from another class, what you can do 46 00:02:43,020 --> 00:02:46,080 inside the method is called a super function. And 47 00:02:46,080 --> 00:02:49,470 this super function is simply going to call a 48 00:02:49,530 --> 00:02:53,670 method that is from the, what we can call the 49 00:02:53,670 --> 00:02:56,940 mother class, okay. So if I do super().__init__, 50 00:02:57,270 --> 00:03:00,570 I'm going to call the constructor from the Robot 51 00:03:00,570 --> 00:03:03,330 class. So I don't need to write this another time 52 00:03:03,360 --> 00:03:06,240 inside this method. Okay. So I call the 53 00:03:06,240 --> 00:03:08,580 constructor from Robot, I initialize with the 54 00:03:08,580 --> 00:03:11,520 name, the version_number that I pass here, okay, 55 00:03:11,520 --> 00:03:15,651 initialize the temperature, and then I add self.reach 56 00:03:15,651 --> 00:03:17,730 is equal to reach, so I add another 57 00:03:17,760 --> 00:03:20,970 attributes, this time specific to RoboticArm. 58 00:03:21,330 --> 00:03:24,210 Okay, so that's it for the constructor. Now I'm 59 00:03:24,210 --> 00:03:28,291 going to add another method. For example, pick 60 00:03:28,291 --> 00:03:30,360 [Author typing] 61 00:03:30,360 --> 00:03:35,670 _object with, so self and then x and y 62 00:03:35,670 --> 00:03:39,690 coordinates. For example, let's just use a print 63 00:03:40,650 --> 00:03:52,560 Pick object on and then plus str(x), and then 64 00:03:52,560 --> 00:04:00,420 plus, let's add a comma here, str(y) plus, and 65 00:04:00,420 --> 00:04:04,890 then close the parentheses, okay. Pick objects on 66 00:04:04,950 --> 00:04:11,280 x and y. And I'm going to do def place_object, 67 00:04:11,820 --> 00:04:17,320 self, x, and y. Okay, and I'm going to do the same thing here. 68 00:04:18,720 --> 00:04:23,280 But instead of Pick, Place, so I can pick and place 69 00:04:23,310 --> 00:04:26,640 an object. That's very simplified for the example 70 00:04:27,480 --> 00:04:30,870 again. So here we have RoboticArm, we have all 71 00:04:30,870 --> 00:04:33,420 the attributes and all the methods of the Robot 72 00:04:33,420 --> 00:04:36,750 class, and we have some specific as you can see 73 00:04:36,750 --> 00:04:39,660 some specific attributes and specific methods, 74 00:04:40,080 --> 00:04:42,600 okay, which are specific to a RoboticArm and 75 00:04:42,600 --> 00:04:46,200 maybe don't apply to a drone or a submarine or any 76 00:04:46,200 --> 00:04:49,080 other kinds of robots. All right. So now I can use 77 00:04:49,080 --> 00:04:52,050 that class. So this is to the simple class, but I 78 00:04:52,050 --> 00:04:56,070 can use that class in my OOP example. What I can 79 00:04:56,070 --> 00:05:02,010 do is I can do from robots import RoboticArm. So 80 00:05:02,010 --> 00:05:06,650 let's keep my_robot here. Let's remove my_robot2, 81 00:05:06,650 --> 00:05:10,740 and I'm going to create my_robotic_arm is 82 00:05:10,740 --> 00:05:13,500 equal to RoboticArm, and I need to give it a 83 00:05:13,500 --> 00:05:18,090 name. So let's call it Bob. A version number, 84 00:05:18,480 --> 00:05:22,710 let's put 4, and reach, let's put 300, for 85 00:05:22,710 --> 00:05:26,407 example. And then what I can do, I can do so 86 00:05:26,407 --> 00:05:31,710 my_robotic_arm.print_info, okay. I don't have 87 00:05:31,710 --> 00:05:34,350 print_info here, I'm going to use the print_info 88 00:05:34,770 --> 00:05:37,830 from the Robot class. And I can use for example, 89 00:05:37,830 --> 00:05:44,160 my_robotic_arm.pick_objects, with let's say, 90 00:05:44,820 --> 00:05:49,903 three and four. And my_robotic_arm.place_objects, 91 00:05:49,903 --> 00:05:54,450 five and six. Let's see what it does. And 92 00:05:54,450 --> 00:05:58,590 as you can see, so we have first the text here, 93 00:05:58,710 --> 00:06:02,940 the print info of my robot, okay R2D2. And then 94 00:06:02,940 --> 00:06:05,790 we have the print info for, so Hello, my name is 95 00:06:05,790 --> 00:06:09,210 Bob, ready to help!. So Bob is the Robotic Arm, Version 96 00:06:09,210 --> 00:06:12,420 number: 4, okay, here. And then you can see Pick 97 00:06:12,450 --> 00:06:15,540 objects on three, four. Place objects on five, 98 00:06:15,540 --> 00:06:19,110 six, okay. And that pick_object is only available 99 00:06:19,110 --> 00:06:22,620 for the RoboticArm not for the normal Robot, and 100 00:06:22,620 --> 00:06:25,530 then well, so you have one level of inheritance. 101 00:06:25,710 --> 00:06:28,140 Now you can create another class if you want that 102 00:06:28,170 --> 00:06:30,960 inherits from the RoboticArm. Okay, you can 103 00:06:30,960 --> 00:06:35,880 create multiple levels of inheritance. And here are the 104 00:06:35,910 --> 00:06:39,030 different examples that you can inherit from 105 00:06:39,030 --> 00:06:42,210 Robot. Let's say you have a class of a Drone or a 106 00:06:42,210 --> 00:06:46,950 Mobile Base, or a Submarine, etc. Each of those 107 00:06:46,950 --> 00:06:50,670 classes can inherit from the Robot class. Alright, 108 00:06:50,670 --> 00:06:53,670 so to recap, that is how you can use the power of 109 00:06:53,670 --> 00:06:57,990 inheritance to create programming blocks on top of 110 00:06:58,080 --> 00:07:01,530 other programming blocks without reinventing the 111 00:07:01,530 --> 00:07:03,083 wheel every time.