One final pillar of hope to go.
We talked about encapsulation, about abstraction and about inheritance.
And then there's this fourth one that has a pretty tough word, polymorphism.
What does that mean?
Well, poly means many and more ism means form so many forms.
Now we know that methods belong to objects, right?
We use the self keyword to act upon the object that got instantiated.
Now in Python, this idea of polymorphism refers to the way in which object classes can share the same
method name.
But those method names can act differently based on what object calls them.
Now, that sounds extremely confusing.
So let's have a look at a code.
We have our user wizard and archer classes.
And as I mentioned with polymorphism, different object classes can share method names.
So we have a TAC here that is shared, but each one does something different based on the attribute.
For example, when I do Wizard one attack.
So let's do that.
Wizard one dot attack.
If I click run I get attacking with power of 60 because well Wizard has a special meaning to its attack
versus Archer.
They're different.
So although they share the same method names because of the object that's calling it, the output is
going to be different.
So we can actually do something quite interesting here.
Let's say that I have Wizard one and then we'll also have Archer one and Archer one will be Robin.
And Robin has 30 hours now.
What I can do here is actually called them in different ways.
For example, I can create an entire new function called player attack.
And this player attacks takes a character.
And in here we can say character, dot, attack.
And now if I run this code and say.
Player attack.
Wizard one.
And I run this.
I'll get attacking with power of one.
If I do play our attack, have Archer in here if I click Run.
You see that the same function gives me a different output, even though we're calling the same thing
because of the object that we pass into it.
Polymorphism.
Another way to demonstrate this is if we do a for loop.
If I do for let's say character in and let's add Wizard one and Archer one and to a list.
And if I print here or let's just run character dot attack.
If I click run here.
Once again, I have two different outputs, even though I'm calling the same method because of the different
objects.
And this is a really powerful concept, right?
Because we're able to customize this according to our specific needs, even if, let's say that the
user had a attack.
Method.
And this default attack method is, let's say, print, do nothing because it's just a user.
Even if I run these and let's say print wizard one.
And let's say dot attack if I click Run.
It's going to override whatever the original attack was because we already have that method in our wizard
class.
But let's say I wanted to have both user and wizard run the attack method.
How can I do this?
Now.
This is something we'll explore a little bit more in the next video.
But for now, I can say user dot attack.
And give it self.
Because I accept user as my parameter in here.
And then I can run user dot attack.
So let's see what happens here.
If I click Run.
I have do nothing and attacking with power of 60.
So polymorphism allows us to have many forms.
It is the ability to redefine methods for these derived classes that is Wizard and Archer.
And an object that gets instantiated can behave in different forms in different ways based on polymorphism.
And this is useful because we are able to modify our classes to our specific needs.
But also not have to repeat ourselves in case we want to use something like attack from user inside
of Wizard.
So there you have it.
The four pillars of OP encapsulation.
Abstraction.
Inheritance and then finally polymorphism.
Now.
No amount of videos or books are going to teach you exactly when to use what.
However, the idea here is to understand that these powers exist with OP.
You're never going to say, Oh, I need to implement polymorphism.
No, most of the time you're just coding along and it happens that you're coding in a way that emphasizes
polymorphisms.
This is just the power of OP, although these are big words.
At the end of the day, we've learned how to use them to structure our code in a way that is efficient
and clean.
And that's the whole point.
It's all about organizing our code.
Now, some of you may have been a little confused when I did this user attack self.
In the next video, I want to explore this a little bit more.
I'll see you in that one.