Hello guys.
So we are going to continue the discussion with respect to Python.
And we are going to continue the object oriented programming series.
And in this video we are going to discuss about polymorphism.
Now polymorphism is a core concept in object oriented programming that allows object of different class
to be treated as objects of a common superclass.
I know there will be a lot of confusion when you read this sentence.
Uh, but just go ahead and read the next line.
And if you are not even understanding over here, don't worry, I will show you a lot of practical example
which will actually help you to understand each and everything.
So it provides you a way to perform a single action in different forms.
Okay.
Poly form ism basically means multiple form okay.
If you split this word it is basically called as multiple form okay.
And here it is written polymorphism is typically achieved through method overriding and interfaces.
So let me just go in and talk about this, and let me just give you a good example to make you understand,
okay.
But it is my duty to probably write this kind of definition so that you also know the definition with
respect to this.
Okay.
Now what does method overriding allows a child class.
So here I'm just going to give you this command C or let me make this or remove this completely.
And and let me make or add a markdown over here.
So here you can see let me just go ahead and execute this method.
Overriding allows the child class to provide a specific implementation of a method that is already defined
in the parent class.
Okay.
That basically means let's say if I'm defining a parent class, okay.
Over there I have defined a method, right?
Uh, let's say the method will be like, uh, if I probably create a dog class, the method is speak.
Okay, so one dog, uh, makes a sound.
Woof woof woof.
Then let's say that I'm inheriting that particular dog with some child class.
Now, that particular dog makes a louder sound.
Woof woof woof woof woof.
So there what we are doing, we are overriding the functionality of that particular method.
Right.
So method overriding is nothing, but it allows a child class to provide a specific implementation of
a method that is already defined in its parent class.
So let me just go ahead and take one specific example.
And here you'll be able to understand.
So first of all I will go ahead and create my base class okay.
Now this base class will be my animal class okay.
So let's say that this is my animal class.
And I'm going to just going to create a function that is called as speak.
And here I have not written any constructor because I'm not defining any attributes.
Okay.
So let's say that I've used speak and with the self parameter and it is just going to return something
like the sound sound of the animal okay.
Something like this.
Now this becomes my base class.
Now let me just go ahead and derive my derive class okay.
Or write down my derive class one.
Now in this derive derived class, let's say I will go ahead and derive or I'll create a child class
which is called as dog and it is basically inheriting from animal.
Okay.
And then we go ahead and define the definition definition speak.
And here we go ahead and define self.
Right now.
See the method is same over here.
Right.
If we are inheriting this particular class, you will be able to see that.
Okay, fine.
This is the method that I have right over here.
And I can also go ahead and return my own definition.
So let's say the dog makes a sound like woof.
Okay, so this is the sound that the dog makes.
So what I have actually done the same method is there but different implementation.
So here method overriding allows a child class to provide a specific implementation of a method that
is already defined in the parent class, so this method is already present over there, but the child
class has provided its own specific definition or implementation right now, let me just go ahead and
create another derived class okay, derived class.
And this time I will go ahead and create a class that is called as Cat and let this get inherited with
animal.
Okay animal.
Now here I can again go ahead and override the functionality of this method.
And here I can say, hey, it returns meow.
Okay.
It returns meow.
Now let's go ahead and define our or create our object.
So here what I'm actually going to do I will just go ahead and write dog is equal to dog.
Okay.
Now if I go ahead and print dog dot speak now which method it is going to just call, it is just going
to make sure that it calls this specific method.
Right.
And similarly, let's say that I go ahead and create my cat okay.
And let's say that here I will just go ahead and print cat dot speak okay.
Now if I go ahead and execute this here, you'll be able to see that hey I am getting when I go ahead
and execute dog dot speak, I'm actually getting woof when I'm uh, executing cat dot speak.
I'm getting meow.
And this is what Mr. Method overriding is all about.
You know, the same method is getting overridden in the derived class with its own implementation.
Right.
And, uh, I can also do one thing.
Okay.
Uh, after doing all these things, you know, I can also create one additional function.
So function that demonstrates.
Demonstrates polymorphism.
Here, what I will do.
I'll go ahead and create a function.
And this is a public function, not inside a class okay.
Not inside any class.
So let's say that I'm going to create animal speak.
And here we will provide an attribute of the class or object of the class okay.
And then we can just go ahead and write print animal dot speak method.
Okay.
Now this animal that I am actually providing, it's an object object of this specific classes.
Now after defining this method I can just go ahead and write print okay.
And I can go ahead and say dog dot speak or let me just go ahead and uh, sorry, it should be animal
underscore speak.
And here I will just be providing dog once.
I probably define this and execute it.
Here you can see that I'm getting hoof right because as soon as I call this function here, we are giving
the object of this particular class.
And inside the same dot speak method is basically getting called right.
So this way also you can probably show polymorphism.
But here what we have done, we have done method overriding.
We have we are just overridden all the functions over here from the base class till the child class.
Okay.
So this was one example over here.
Now let me just show you one more, another way where I will be showing you polymorphism with functions
and methods.
Functions and methods okay.
Now polymorphism can be also implemented using function and methods to process objects of different
types in the same way.
Right?
So here let me just go ahead and show you one base class.
Let me just go ahead and create one base class here.
The class will name will be shape.
Let's say that I'm going to define shape.
Now let me just go ahead and write definition area.
So I'm going to create a function which is called as area.
And here I'm going to give my self keyword okay self Self keyword.
Now let's go ahead and return the area of the area of the figure.
Okay I'm just going to probably give a print message.
And this is basically present in the base class okay.
Now what I will do I will go ahead and create my derived class.
Now see this okay.
Again I here I'll also be implementing function overriding okay.
So this will be my derived class one okay.
Let's say that I'm going to probably create a rectangle rectangle.
And you know rectangle will inherit shape okay.
And you know if for rectangle definitely what we require in order to calculate the area, it is nothing
but width and height.
So I'm just going to create my init method.
And here I'm going to give self comma width comma height.
This is what I'm going to pass in the runtime.
I will go inside this.
Now let me just go ahead and create my parameters or attributes.
That is nothing but self dot width.
Self dot height.
And then we'll go to the next sentence.
Oh sorry.
This should be assigned to width.
Okay.
Then we will go ahead with self dot height which will be my another attribute.
And this I will assign it to my height okay.
Now I will go ahead and call this area function.
Right.
Now here what I am doing I am again I'm overriding this area method because the functionality will be
little bit different over here.
Right.
How to calculate a area of a rectangle.
It is nothing but just return self dot width.
Self dot width.
multiplied by self dot height.
Self dot height.
Right.
This is what we are specifically doing.
Now let me just go ahead and call the derived class two.
Okay so here we have completely redefined or we have overridden the entire function.
Right.
So it's like all the function are basically having multiple forms as we keep on, uh, making sure that
once our derived class inherits the parent class, we are basically redefining this entire function
or we are overriding the functionality.
Okay.
Now let me just go ahead and create another derived class.
And this time I'm going to create it as circle.
Okay.
This will again inherit shape okay.
Shape.
So let me just go ahead and inherit shape.
And now this time, you know, I'll be using an init function and this will be a constructor.
And here what I require in order to calculate the in order to calculate the area of a circle I just
require radius.
Right.
So here I'm basically going to write self comma radius okay.
Now let me just go ahead and write self dot radius is equal to radius right.
So here you will be able to see that I have able to just get this particular parameter.
I am able to create it now here also the area definition will be the function will be different right.
So how do I calculate the area over here.
I will just say return right.
It is nothing but pi r square right?
Pi is nothing but 3.14 multiplied by the radius into sorry self dot radius self dot radius multiplied
by self dot radius pi r square.
Right.
Done.
And this will basically give me the area.
Now what I will do along with this, I will just go ahead and write a function that demonstrates polymorphism.
And here is what I will be writing.
So here let me just go ahead and write print underscore area.
And here I'm actually going to give me give the shape which will be an object okay of a specific class.
And we will just go ahead and print with my f string.
The area is nothing but shape dot area.
Right shape dot area.
Which will be my function.
Okay.
Now this is what we are going to just print it.
Right.
Now let me just go ahead and create the objects.
So first of all what I'm actually going to do I'm going to create a rectangle object.
Go ahead and use rectangle right.
Basically use four comma five.
In case of circle I just need to provide my radius.
So let me just go ahead and define circle with radius.
Radius is let's say three.
Now if I want to go ahead and just call the area function I will just go ahead and write print underscore
area.
And I will give the object that is rectangle.
Okay rectangle.
And similarly I will go ahead and call print underscore area where I will be giving circle right.
And this is how we execute it.
And here you can see the area okay.
The area is 20 and 28.25.
So this is the area with respect to uh, you know uh the, the, the, the, the area of the rectangle
which we basically write length into breadth.
So nothing but four into five.
And if I go ahead and calculate pi r square.
So I'm actually going to get 28.25.
Right.
So this is one amazing example.
And here you can see that we have used functions.
We have defined another function that demonstrates polymorphism over here.
And uh this is one of the amazing example with respect to this okay.
And here also we are basically playing with the same method name but with different different definition.
Okay.
Now let me just, uh, introduce you to see, uh, I've already written over here something like this.
Right.
Um, polymorphism is typically achieved through method overriding and interfaces.
Now let's talk about this.
Interfaces.
What is interfaces in other programming languages we basically say interfaces in Python.
We say it as abstract base class.
Now what exactly it is.
Let's discuss about it.
So polymorphism with abstract base classes Okay.
Now abstract base class.
Uh, if I really want to define it.
Okay.
It's very simple.
Let's go ahead and see the definition.
I'll paste it over here.
And let me just go ahead and write this okay.
Uh, important topic altogether.
Okay.
Abstract base class are used to define common methods for a group of related objects.
They can enforce that derived class, implement a particular method promoting consistency across the
different implementation.
Okay, so abstract base class.
That is the reason we say it as ABC.
Okay let's do one thing okay.
Let's let's show you an example over here.
Uh I will be defining two important things.
First I will write from ABC.
Okay.
Now what is this ABC?
We are basically using abstract base class.
We are going to import ABC.
Okay.
Along with that we will be importing abstract method okay.
This abstract method is a kind of decorator okay I'll be talking about what exactly is decorator as
we go ahead.
Right.
But just consider right now this is what, uh, abstract class is all about.
You know, it is it is just like it is just used to define a common methods.
Okay.
So I will just go ahead and define an abstract class.
How do I define an abstract class.
This is what I'm going to show you.
Abstract class okay.
Now um abstract class here I'll say hey, uh, let's create a class called as vehicle.
Okay.
And this I'm going to inherit to the abstract class, which will be like an empty class itself.
Right?
So, uh.
this abstract class, afterwards, I define it, I will be defining abstract method.
Also see, in order to just give you a simple definition of abstract class, it is like a complete empty
class.
You know, there, uh, if you are defining also method, it will be initially empty.
Okay.
So that is the reason why we have written that abstract base classes are used to define common methods
for a group of related objects.
Here no definition will be there much.
Okay.
So, uh, with respect to this, uh, now I have created this particular class.
I'm not putting any information, but let me just go ahead and create an abstract method.
Okay?
Abstract method is just like an empty method, but it is kind of common class.
That is probably going to happen in each and every class, because we are going to inherit this kind
of abstract class onto this.
Right.
So here what I'm going to write, I'll just go ahead and write define start engine.
So this will basically be my function.
And here I'm going to use my self keyword.
And since it is abstract it needs to be empty.
So I am going to just write pass okay.
So this is how I have actually created my abstract class.
Along with that I am also creating my abstract method called as Start Engine.
Now what I will do for my derived class one I will inherit this.
Okay, so let me just take class car And here I'm just going to use vehicle okay.
Now you know that I have defined my abstract method which is completely empty.
Now I can since I'm inheriting vehicle, I'll also be inheriting this particular method and I can define
my own function.
So that is the reason we'll go ahead and write definition start underscore engine okay.
Start underscore engine self.
And we are just going to return over here Car engine started okay.
Some information.
Car engine started.
Okay.
And here you can define your own init method also inside this particular class also.
Right.
Define any number of parameters that you want okay.
Now uh similarly if I go ahead and create my derived class two, you'll be seeing hey, let's go ahead
and create this particular class called as Motor cycle.
Okay.
And here I'm actually going to give my vehicle okay vehicle class.
And let's go ahead and inherit it.
And uh if I go ahead and write definition start underscore engine here I'm just going to return motorcycle
dot sorry motorcycle engine started.
So here you can see this function is having a different definition inside motor class motorcycle class.
And it has a different definition inside the child class.
Okay then uh let me just go ahead and create the object of the class.
So create objects of car and motorcycle.
Now see I will just go ahead and create it.
Okay.
And we can do the same implementation like this also.
Right.
So I have inherited over here.
Let me just go ahead and define one function that will demonstrate my entire polymorphism.
And now I can just go ahead and start this vehicle and just let me define my car.
And if I go ahead and execute it, you can see that automatically it is being able to call this called
as uh, start engine.
And here we are able to display it.
So here I have actually shown you polymorphism with the help of method overriding.
Along with that we have also seen with the help of abstract class.
So this two you definitely need to know.
And uh, again uh, ending this session I will try to provide some conclusion which is a good habit.
Okay.
So here with respect to the conclusion, you should know why polymorphism is important.
It is a powerful feature that allows the flexibility and integration in code design.
It enables a single function to handle objects a different class, each with its own implementation
of a method.
By understanding applying polymorphism, you can create extensible and maintainable object oriented
programs.
Okay, but here we are just seeing that what all features it has with respect to polymorphism.
Right.
So yes this was it for my side.
I hope you like this particular video.
I'll see you all in the next video.
Thank you.
Take care.