High in this session, we will discuss about polymorphism, so we have known that Wii functions can
take different arguments.
Methods belong to the object they act on, right.
So we have methods which act on top of different object.
So when we create a class, the class will contain different functions.
And only these functions are called methods.
And these methods will be working on the objects.
Right.
Whatever task I will be doing, whatever task my method will be performing, that task will be performed
on top of my object.
OK, now in Python polymorphism refers to the way in which different object classes can share the same
method name and those methods can be called from the same place, even though a variety of different
objects might be Boston.
OK, so I can barson different types of object and my method can be called from the same place so I
can have the same method acting differently for different types of object.
OK, so how do we explain that.
So basically polymorphism is a property of an object which allows it to take multiple form.
OK, so when an object can take multiple forms and when we can work on different forms of an object
that is called polymorphism, so it allows us to define same method in different classes so I can have
the same method.
Let's say I can have the same method as speak, OK, and this week can be present in case of dog also
and in case of Catalyser.
But how the output comes out to be would be different.
So a dog would say, Wolf, while a dog would say meow.
So those are the differences.
So I have the same method speak, but we acted differently in case depending on if I'm calling in dog
object or I'm calling the cat object.
Right.
So this is also known as method overriding the veto override, a particular method.
We change how a method works.
OK, now an example of this would be so we use GPS for navigating around.
So when we come across many different routes for same destination, depending on the traffic from programming
point of view, this is called polymorphism basically suggests us different route.
So that is called polymorphism.
So when same thing and have different solutions, which is called polymorphism.
OK, so here we have, let's see, ADR method, which will have different implementations for specific
rectangular cube.
So for all of these, the method area will remain the same, but how it acts would change.
So for Sfeir it will calculate the area differently for so-called beguilingly the.
So that is what polymorphism is and it is one such methodology and Opsware one task and be performed
in several different ways.
So that is exactly what polymorphism is.
Then we can use the same name for a method, but it will act differently based on what my object is,
the nature of my object.
So based on the nature of our flag differently.
OK, so let's go ahead with the coding part of it.
OK.
So here I'm creating a class, this class is called your dog.
And inside this, I will define the method.
So first of all, I define in it and it will take in self, which is by default and it will take in
the next.
It will say self, not name is equal to a name.
And let me define another method.
Say this and I will speak so in speak.
I will take says I don't need anything else.
I'm inside this.
I will say that Don and I will say self dog name plus
says if I put this in quotes.
So this is one of my string.
Similarly, I create a similar class for CAT.
So when I create this class for CAT and change its name for From Dog to God and everything else remains
the same.
But here the cat will instead see me.
OK, now you can see here the speaker is present in case of class dog, and this method is saying,
Wolf, as a result and in case of Gatica saying now and we have the same method, the name, OK, so
we are using the same method names.
So when I create, let's say, some examples, I'm creating a dog Dominico and I see a dog and I give
it a name, Nikou.
And similarly, I create a cat Feliks and I'll give it the name, the same as that.
And after that, when we will simply print, we print Nicole don't speak.
And similarly, we print.
Felix, don't speak, and we will have to run this method, so we'll run this method and when we will
run this, what will happen?
So it gives me Niko says, well, Felix is like in outer space here so that it looks a little better.
So you can see.
So what we have achieved here is we have achieved the same method.
We have implemented the same method of name, and we have achieved a different behavior out of those.
So I can have any number of animals.
I can have double got a cow and horses and pigs and whatever, and all of those can have their different
sound.
And for all I can use the same method and it will be so very convenient to use this.
And I will not be confused by finding out while remembering what me might have used.
So this is the convenience which will provide you.
So here we have a dog class and a class and each has a dog speak method when called the object as we
speak.
So we are calling these objects as dog speak and they will retain redounded unique object.
So the result will be unique based on what object they have.
So similarly we can have different things so we can implement these differently.
I'll send them out during the code so you will find those in code.
Now, in case of classes, it is a more common practice where we can use abstract classes and inheritance.
So we have seen inheritance or you're right, so here we have seen an inheritance.
So here we have a class animal and we have created some particular method here.
And this method is being overridden here in the US.
But there could be a scenario where I want to create some method in case of the best in class and I
want to implement those only in the class or something like that.
So that is the place where we can use abstract class or an abstract class is one that never expected
to be instantiated.
So it will not expect any instantiation of.
So we will never have an animal object.
Only dog and cat object will be present, although dog and cat are derived from the animals.
So we will be creating something like that.
We will be creating dog and cat classes which will be derived from the animal flies.
But in case of animal class, we won't instantiate that ever.
So let's do that without losing any time.
So I will say class
animal and I see the.
So what I have done here is I have created this class animal inside this class, I have given the definition
for in it and I have given the attribute name inside this.
After that, I have given the function, the method to speak and for this method speak.
I am saying that the reason is not implemented error, saying that subclause must be implementing this
abstract method.
OK, so abstract method is defined by conventional.
So this is defined like this only.
OK, this will be defined like this.
And after that I'm creating this class and this guy does just how we have implemented it.
Olio in the same way we have simply said to the class dog and we have said def speak, self written,
self taught name Seawolf said don't mean meal.
And then I see pre the class create an object dog and create an object for God.
And then I bring to the door and I start to speak.
So let me run this.
So it gives me Vidor's is Wolf ISIS ISIS's.
So here what we have done is we have created this.
Now let me do something like let me not define this here.
So if I would not define these definitions.
So let me not define it for just one class.
So here it is seeing that it is expecting some intended block.
So let me put parse here, first of all, so you can see it is giving out the not implemented error
in same subclass must be implemented abstract method.
So it is saying that it needs to implement this.
OK, so that is what this means.
Or an abstract class.
We can create an abstract class by defining this is not implemented error and that we make it mandatory
for us to implement it under the child classes.
OK, so in real life, examples of what polymorphism we can have, something like when we are opening
different file types so different tools are needed to display so would be difficult.
Exactly.
Exactly right.
So in those case we can have a different kind of file.
So different kind of handling for different files and they can have different methods for that.
We can use the same method, open file, but each file will be opened differently based on the life
from it.
Just in another example would be when we are adding different object.
So in that case, the class operator will perform an automatic operation by when we are adding the strings,
it will this act as a concatenation.
Right.
So these are different examples that we have now.
Apart from that, the next topic which we have is encapsulation.
So we will be discussing that in the next session.