So we've talked about encapsulation, about abstraction.
What about the third pillar of OP?
It's called inheritance.
And inheritance allows new objects to take on the properties of existing objects.
So you can inherit classes.
Let's have a look at an example.
Let's remove the previous code that we've had and go back to our game example.
We want to create a new game and we have different types of users.
So we have users that can be wizards, that can be else, that can be, I don't know, archers, but
all these users have some common functionality and each one of the sub users like wizards, elves,
archers have some common shared functionality, but then again, different maybe attacks.
So using OP we can actually do inheritance.
For example, let's say we have a class user here and this user will have, let's say, a sign in function.
Or sign in method once again it's going to receive self and then we'll just for now assume that we have
some sort of a sign in here.
So let's just say for now print blogged.
And because in order to play a game, they have to be logged in.
Now, you might be wondering, ho, ho ho, where is the init method here?
Shouldn't we have that init method that gets run first?
Well, we could, but if we don't have any variables or attributes that we want to assign to the user,
well, in that case we wouldn't need an A method.
So for now, we'll just say that we don't need there's nothing that the user has other than this sign
in method.
Now let's say we want to create a bunch of subclasses so that we have a class wizard.
And for now, we'll just do a pass here so that we don't have an empty class, but it will still allow
us to build it.
And then let's say we also have a class.
Archer that will pass through as well.
Oh.
Make sure we have brackets here.
Now, here's the thing.
Ideally, all these wizards and archers are users as well.
Right.
So we have users.
And these users can have multiple forms.
They can be wizards.
They can be archers.
They can be ogres.
All the characters in our game.
But all of them, let's say, have to be signed in first.
How can we make sure that all of these users or classes also have access to sign in?
Well, we can use inheritance.
And how do we do inheritance?
Quite easily, actually.
All we do is in the bracket.
Pass the parent class that we want to inherit from.
So let's say I do user here.
Now let's click run here.
No airs.
Everything is looking good.
And by the way, you see the green line here tells me that indentation is not a multiple of four because
indentation should be four in Python.
So we can just do this.
But I like keeping it smaller just so it's easier to read.
Okay.
So let's instantiate a class here.
I'm going to create a wizard one and then say.
Wizard and run that wizard class.
Now, if I do a print here and I do, let's say print wizard.
If I click Run.
I see that the wizard object has been created.
And what if I do not sign in?
I see that.
Yeah, it's actually there.
If I do sign in and click Run.
Hey, I'm logged in.
How cool is that?
I inherited from the user class this functionality.
And this is really, really powerful because now I can extend my wizard.
Right.
So let's say that this wizard has an init.
Method.
Which will be run when the wizard is created.
And we'll have a.
Name.
And power that we get.
And in here we'll assign self name to equal name and then self dot power to equal power.
And let's also add another method.
Calling it attack.
And this attack method will simply print an F string that will say.
Attacking with.
Power of.
Let's say self power.
And then we're going to do the same for the Archer.
So I'm going to copy this code.
Paste it in here.
And the archer will have name.
And let's say instead of power, we'll say that it has, let's say.
Number of arrows.
And number of arrows.
Will be the new attribute that it gets.
And then the attack will have well, attacking with arrows.
And then we'll have arrows left.
An arrows left will just say number of arrows and we can do something fun where the arrows decrease
as every time you attack.
Okay.
So now that we have this, let's make this a little bit smaller just so you can see.
There you go.
Now that we have this, let's instantiate the first wizard.
With.
Let's say Merlin, with a power of 50.
And then the archer.
We'll be instantiated.
As Robin with, let's say 500 arrows or let's do 100 arrows.
So now the wizard and the archer are going to have.
Well, they'll have the sign in.
But they'll also have the attack, but unique to their individual.
Classes.
And they'll also have different properties.
Let's have a look.
If I was to do Wizard Dot.
Mr. Wizard.
One dot.
Now we'll say.
Attack.
And let me just turn back.
My code intelligence.
We'll see that we have a few errors.
Here.
What's the what's it say.
Undefined name self and that's because I forgot self in the brackets so you remember we always have
to give the first parameter self.
If we scroll down we go to Wizard Dot, we see that we have name, power and attack.
If I click attack here and I run.
See that I'm attacking with power of 50.
If I do, archer one dot attack.
In that case, I'll get attacking with arrows.
Arrows left 100.
But both of these have the sign in function at the same time.
How cool is that?
This is the power of inheritance.
We're keeping our code dry, right?
We're abstracting away the part of the code that they both share, but then changing things according
to what each one needs.
So for example, I could have different methods and properties on Wizard than the Archer, but also
have a shared user's user functionality that they have.
And this way it keeps our code organized and clean.
Now let's bring back the zoom in just so you can see it clear.
Now, this idea of inheritance is really, really powerful.
And the key here is that we have a parent class and children classes.
Now, sometimes these children classes are called subclasses or derived classes because they're subclasses
of user or derived from the user class.
Let's explore this topic a little bit more in the next video.