Great, you have your class that is now complete.
Now let's say you want to create a class for a
robotic arm. This class is actually a Robot, but
with more functionalities, okay that are specific
to robotic arm. So to create the robotic arm
class, you will need to rewrite most of the Robot
class code here, and there is a way to avoid doing
that and directly create the robotic arm class on
top of this Robot class. This is called
inheritance. And let's see what this is and how to
do that directly with a code example. So I'm going
to create, I'm going to stay in my robot.py file
here, and I'm going to create another class. So I
go back to the line with zero indentation, class RoboticArm.
Okay, and I'm going to do things a little
bit different, I'm going to open parentheses and
put Robot like this. This means that the RoboticArm.
class will inherit from the Robot class. So
basically, it will inherit from all the attributes
and all the methods. So basically, if I just write
this, and then if I do pass, which is basically
the Python keyword, to do nothing, for the RoboticArm
I can create a RoboticArm with the name,
version number, it's also going to have an
internal temperature, and I can call say_hi, init_hardware,
and print_info, and that's going to work.
Quite convenient, right? And we can also add more
attributes and methods inside the RoboticArm
class, which are this time specific to a RoboticArm,
and maybe don't apply to any kind of robots.
Okay, so I'm going to here, what am I going to do,
I'm going to def __init__. So I'm going to create a
constructor, and I'm going to do self name,
version_number, and reach. So let's say that to
initialize the RoboticArm, I need to give a name,
version_number, so just like I did here, and a
reach in millimeters, for example. So I have my
constructor here with the parameters, and now what I'm
going to do, in the constructor I'm going to call
super open, closed parenthesis .__init__, okay, with
name, version_number, and then I'm going to do
self.reach is equal to reach. So what did I do
here? Well, for any class, basically, you're going
to create a constructor, okay. And here, if a
class inherits from another class, what you can do
inside the method is called a super function. And
this super function is simply going to call a
method that is from the, what we can call the
mother class, okay. So if I do super().__init__,
I'm going to call the constructor from the Robot
class. So I don't need to write this another time
inside this method. Okay. So I call the
constructor from Robot, I initialize with the
name, the version_number that I pass here, okay,
initialize the temperature, and then I add self.reach
is equal to reach, so I add another
attributes, this time specific to RoboticArm.
Okay, so that's it for the constructor. Now I'm
going to add another method. For example, pick
[Author typing]
_object with, so self and then x and y
coordinates. For example, let's just use a print
Pick object on and then plus str(x), and then
plus, let's add a comma here, str(y) plus, and
then close the parentheses, okay. Pick objects on
x and y. And I'm going to do def place_object,
self, x, and y. Okay, and I'm going to do the same thing here.
But instead of Pick, Place, so I can pick and place
an object. That's very simplified for the example
again. So here we have RoboticArm, we have all
the attributes and all the methods of the Robot
class, and we have some specific as you can see
some specific attributes and specific methods,
okay, which are specific to a RoboticArm and
maybe don't apply to a drone or a submarine or any
other kinds of robots. All right. So now I can use
that class. So this is to the simple class, but I
can use that class in my OOP example. What I can
do is I can do from robots import RoboticArm. So
let's keep my_robot here. Let's remove my_robot2,
and I'm going to create my_robotic_arm is
equal to RoboticArm, and I need to give it a
name. So let's call it Bob. A version number,
let's put 4, and reach, let's put 300, for
example. And then what I can do, I can do so
my_robotic_arm.print_info, okay. I don't have
print_info here, I'm going to use the print_info
from the Robot class. And I can use for example,
my_robotic_arm.pick_objects, with let's say,
three and four. And my_robotic_arm.place_objects,
five and six. Let's see what it does. And
as you can see, so we have first the text here,
the print info of my robot, okay R2D2. And then
we have the print info for, so Hello, my name is
Bob, ready to help!. So Bob is the Robotic Arm, Version
number: 4, okay, here. And then you can see Pick
objects on three, four. Place objects on five,
six, okay. And that pick_object is only available
for the RoboticArm not for the normal Robot, and
then well, so you have one level of inheritance.
Now you can create another class if you want that
inherits from the RoboticArm. Okay, you can
create multiple levels of inheritance. And here are the
different examples that you can inherit from
Robot. Let's say you have a class of a Drone or a
Mobile Base, or a Submarine, etc. Each of those
classes can inherit from the Robot class. Alright,
so to recap, that is how you can use the power of
inheritance to create programming blocks on top of
other programming blocks without reinventing the
wheel every time.