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 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 robotic arm. 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 robotic arm 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 robotic arm I can create a robotic
arm with a name, a version number, it's also going
to have an internal temperature and I can call sayHi
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 robotic
arm class which are this time specific to a
robotic arm and maybe don't apply to any kind of
robot. Okay so I'm going to here, what I'm going
to do, I'm going to do 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 robotic arm I need to give a name,
a 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 close parentheses dot init okay
with name, version number and then I'm going to
do self dot reach is equal to reach. So what did I
do here? Well for any class basically you are going
to create a constructor okay and here if a class
inherits from another class what you can do inside
a 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 dot 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 dot reach is equal to reach so I
add another attribute this time specific to robotic
arm okay. So that's it for the constructor now
I'm going to add another method for example pick.
object with uh so self and then x and y coordinate
for example let's just use a print pick object on
and then plus string x and then plus let's add a comma here.
str y plus and then close the parentheses okay
pick object 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 okay
that's very simplified for the example again.
So here with robotic arm 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 robotic arm and maybe don't
apply to a drone or a submarine or any other kind
of robot. All right and now I can use that class
so this is still a simple class but I can use that
class in my OOP example what I can do is I can
do from robot import robotic arm so let's keep
my robot here let's remove my robot two and I'm
going to create my robotic arm is equal to robotic
arm and I need to give a name so let's call it
Bob a version number let's put four and reach
let's put 300 for example and then what I can do
I can do so my robotic arm dot print info okay
I don't have print info here but I'm going to use the print info from the robot class
and I can use for example my robotic arm dot pick object with let's say
three and four and my robotic arm dot 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 four okay here and then you can
see pick object on three four place object on five
six okay and that pick object is only available
for the robotic arm 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 robotic arm okay you can
create multiple levels of inheritance and here as
different examples that you can inherit from
robot let's say you have a class for a drone
or a mobile base or a submarine etc each of
those classes can inherit from the robot class
all right 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.