Hello guys. Welcome to LearnVern. In the last topic, we saw Inheritance, where we saw how Inheritance works in Python, how Parent Class, Base Class work, how 1 class’s object accesses the functionality of other objects and how we inherit from Parent Class to Child Class.
Today we will see Polymorphism.
First we will learn the theoretical part and then the practical one on how Polymorphism works in Python.
First & foremost, what is Polymorphism?
Polymorphism has many forms of the same name.
Suppose we have a class in which there are 3 to 4 children of the same name, students having the same name.
That is 1 type of Polymorphism. Polymorphism means having the same name but multiple forms.
Types of Polymorphism. There are 2 types of Polymorphism.
One is Compile Time Polymorphism & other is Runtime Polymorphism.
There are sub-parts to it. In Compile, it is Method Overloading.
And in Runtime, there is Method Overriding.
First of all, what is Method Overloading?
Method Overloading is a Compile Time Polymorphism.
Why is it called Compile Time?
Because they are having the same name methods in the same class but different arguments.
Suppose you didn’t pass any argument in 1 method, then you made another method with the same name and passed 1 argument, followed by a 3rd method & passed 2 arguments in it.
Here, you keep the same name of the method, even the same class, but the signature is different.
In Method Overloading, Inheritance is not required.
First of all, we will see how method overloading works practically.
Jupyter is open for the practicals.
I’ll markdown Method Overloading.
What is the definition of Method Overloading?
That the name of the Method should be the same.
And it should be in the same class as well.
Meaning the same name methods should be made under the same class only but the arguments should be different.
I made a class here named MO.
I’ll make a method in here which is myfunction and pass Self in here.
Self is a very must step to pass.
When I explained about class in the Class video, I told you about this.
Those of you who don’t know, please watch the Class & Object video.
Okay I made a function here with no arguments.
Print Function with No Arguments.
So this is our No Argument Function as no arguments are passed in this.
I’ll make a second function in the same class which is myfunction just like above.
I’ll pass Self here and A.
I’ll pass an argument here & write Function with 1 argument.
So what type of function is this? 1 argument function.
I made 1 more function which is myfunction, in which I took A,B.
I have 2 arguments here.
Print Function with 2 arguments.
2 arguments Function.
There is only 1 class, functions have the same name & our argument is different.
Meaning our definition of Method Overloading suits here perfectly.
Now we have to call the functions.
We will make objects here.
Such as M. I’ll write here Creating an object of Class MO.
I kept the name of the object M. And this is my class name.
Now I want to call my 2nd function, which is the 1 argument function.
M.myfunction. And I passed an argument here.
First of all we will run it to see what the output is.
We got an error in output. It is a TypeError.
It is saying that myfunction is missing 1 required positional argument.
But we passed 1 argument only. And we want to call this function only.
We have written the right thing here.
So it should have been called.
So we will follow what this is saying & pass 1 more argument here. 20.
Now when I run it, a function with 2 arguments was run.
But we wanted to call a function with 1 argument, and that did not get called.
What could be the reason?
I’ll write the reason here in Note. Method Overloading is not supported in Python because Python is an Interpreted language.
Meaning this one will run first, it will run line by line.
So next will be the 2nd one.
But when it was time of the 3rd, as we didn’t make any function after the 3rd, the interpreter stopped right here.
That is why it told us that it is a type error and 1 argument is missing.
Now see when I remove 1 argument, it says one argument B is missing.
But we have not passed B in this function..
Then for what function did this error show up?
Our interpreter stopped at the last method, that is why the error is shown.
But if you pass a 2nd argument, it will allow.
That is why Python does not support Method Overloading anywhere.
Since Python is an interpreted language.
Okay? Did you all understand how Method Overloading works in Python and why is it not supported in Python?
Moving onto Method Overriding.
It is 2nd Polymorphism which is Runtime Polymorphism.
Method Overriding is a Runtime Polymorphism.
What does it mean? It means it has all the same methods, but into different classes yet the arguments are also the same.
In Method Overloading, we used to take only a single class and made all the methods inside it.
But here, you’ll have to take different classes and in those different classes, same name methods will be made.
And the signatures will also be the same as well.
In Method Overriding, Inheritance is compulsory.
Then only the Method Overriding will successfully take place in Python.
Understood how Method Overriding works?
Moving to practicals to make you understand clearly.
I opened Jupyter, I’ll take another cell here & markdown.
Method Overriding says that the class should be different, the names should be same & the arguments should be the same too.
Inheritance should be the same too.
Come on, let’s follow the same definition.
I’ll make a class here. MO1.
And make a function myfunction. Pass Self & an argument.
I’ll write here Class MO1 Function Called.
I’ll make another class in the same way that is Class MO2.
I’ll give the same name function & the same argument as above.
Give the same single argument as before.
Print Class MO2 Function Called.
Now I’ll make a 3rd Class here which is MO3.
And make a function just like the 2 before & the signature will also be the same.
Print Class MO3 Function Called.
All the 3 classes are ready.
All the 3 have the same name function & same parameters that is, the same arguments.
Now I have to apply Inheritance.
This is our Parent Class, this is our Child Class because we are going to inherit MO1.
This is our Child Class as well.
Now I will inherit here. I’ll inherit MO1 here.
And inherit MO2 here.
What did we do here? We inherited MO1 into MO2 and inherited MO2 in MO3.
So our object will be created of MO3.
Creating an object of Class MO3.
I took an object named M and created MO3 named Object.
Object of MO3.
Now I will access all the methods of my class through M.
As we have selected inheritance, we can inherit in Child’s Class.
After inheriting, we can access the Parent Class properties.
What type of Inheritance is this? This is Multilevel Inheritance.
Multilevel Inheritance. We saw about Inheritance in the last topic only.
In which we saw Multilevel Inheritance.
If you’ve not seen that, kindly see the Inheritance video first.
Now I have an object named M here.
And I’ll call myfunction through M & pass 10 here.
I’ll pass an argument here because I have passed one in every function.
Now I’ll run it & here only the MO3 function got called.
But we want to access all the functions, right?
I’ll show you how to do it…
Here we use a Super Method.
But what is the Super Method?
Super Method is written in the immediate Child Class.
So I’ll comment here “Super Method Calling the method of Class MO2”.
How will we write it? Super .(dot) the function is here.
I’ll pass the argument and as soon as I run it, the MO2 function gets called as well.
We have to write the same thing here in MO2 as well.
Because we want to call the MO1 function as well.
I’ll edit here to “Super Method Calling the method of Class MO1”.
We don’t need to write in Class MO1 because it is a parent class.
You have to use the Super Method only & only in Child Class.
Now when I run it, all the 3 methods are getting called. Alright…
So this is how Method Overriding works in Python.
In which we can use the Super Method to call the Parent Class Methods.
So this is Polymorphism, in which we saw 2 topics which are Method Overloading & Method Overriding.
In the next video we will see Abstraction & how it is used in Python.
Share a personalized message with your friends.