Another very important concept of object oriented programming
is going to be the concept of Inheritance.
And just like in the English language, we know Inheritance to be when
you know something is passed on from one person to another, right?
So
in Python, we can actually create specialized versions of a class
without even having to rewrite code,
we can basically create a child class that can
inherit attributes and methods from a parent class.
So I want to give you an example in here since we're dealing with the class user
obviously in systems and networks, we have different levels of permissions,
right?
A regular user may be able to access certain kinds of files and then print those files
while an admin user
will be able to do those exact same things that a regular user can.
But because they're an admin user,
they will have extra permissions like being able to say for example,
edit those files, right?
So
what I'm gonna do right now is I'm gonna create a class for the regular user.
But then
I will create another class which will, which will be the admin user
that will inherit
the methods from the regular user. So over here
we have a class user
and now I'm defining one basic attribute
and that's going to be the username. OK?
So now the next step would just be to simply say self dot username
equals
equals. And then of course, user name.
Now I want to create a method
specifically for our user over here. So I'm gonna say define
and let's say a regular user can log in, right? They can log in. So I'm gonna say log in
and now in brackets,
I'm gonna say self,
OK?
And then when they log in, let's just do something,
let's say print and then self username
has logged
in. Let me say has,
has some space in here,
has logged in.
OK?
So far what we've done here is we've created the user class
with the attribute username. We've also assigned a method called login
that will basically print out the user's name
has logged in.
However,
I am now
gonna create
another class that's going to inherit
not only the attributes username from, from the user class,
but also the method log in.
How am I gonna do this? I'm gonna come over here
and then notice the intent.
I'm all the way out.
Now, I'm gonna say class
and now provide the name of this new class. So I'm gonna say admin
and then we can even say admin user as an example. OK?
Actually let me just stick with admin. OK? So admin and now in brackets,
I'm gonna say user
colon.
So right over here on line nine,
we have created a new class of a user called admin user
that's going to inherit
the attributes, username. And then the method log in from the user class.
I'm gonna go one step further and now add
an additional method because this is an admin user,
they can do extra things.
So I'm gonna say define
and let's call this access
on those call logs because they're an admin,
they can access the logs. So I'm gonna say self call on
and now let's just print
uh print
and then in brackets,
I can use F string and let's say something like uh
the self
dot
username.
OK?
Is accessing the logs
and
there it is and there you go, we are done.
So now what I'm gonna do is I'm gonna create our admin user.
So I'm gonna say admin
equals and now in capital admin user
and then in broadcast, let's provide the username,
admin.
OK?
And now
let us run
the methods.
So I'm gonna say admin
dot log in
brackets.
And let's also run the second method which is gonna be admin dot
access underscore logs
and there it is.
So now let's see if this will actually work. Let's run
and 0000
I have another error
admin. Oh,
I do apologize.
It should be admin over here. I got carried away.
Admin equals admin and then the username admin.
I do apologize. So let me move on the program one more time and there you go. All right.
So admin has logged in
and now admin is accessing the logs and
that's basically it.
So we didn't have to specify that the admin user, the class admin,
it's gonna have
the parameter or the attribute username or the method I log in
all we had to do was to indicate to Python that,
hey,
this new class admin is actually inheriting
the attributes and methods of the original
user class.
And that's Inheritance for you. Thank you for watching the video.
I will see you in the next class.