In this tutorial we will talk about polymorphism
in object oriented programming.
The term polymorphism refers to the ability of an object to attach the cord to the type of the data
it is processing so let's understand polymorphism.
Using example.
Let us add two numbers two and three and I'm using a plus operator to perform the addition operation
the interpreter to Don's file this case.
Now let us concatenate two strings hello and whoa.
And if I want to concatenate two strings I have to use the plus operator again and the interpreter in
this case
then we concatenate using the plus operator.
This is what the interpreter returns.
Now I want to concatenate to lis in order to concatenate I have to use the plus operator again and the
output would be one two three.
And this is the output.
If you observe carefully the actions performed by the plus operator the addition operator the different
input parameters.
So then we pass to indigenous to the plus operator.
It is doing a mathematical operation when we are passing two strings to the operator to the plus operator.
It is concatenated ing both of these strings.
Similarly when we pass to list to the plus operator when we want to continue to list using the plus
operator it is concatenate taking both the list into a single list.
No let us come back to the definition or polymorphism the term polymorphism refers to the ability of
an object to adapt to go to the type of data it is processing and an object may provide different implementations
of one of its methods depending on the type of the input parameters when we want to perform addition
between two integers 2 and 3 We know 2 and 3 are instances of the built an integer class and when we
want to perform this addition operation Python actually looks for a method in the integer class which
is the underscored underscore and underscored underscore and this method defines the procedure for performing
addition operation on numbers similarly when we want to concatenate two strings using the plus operator
we know how low and world instances of these string class which is built in class when we want to concatenate
these two strings Python looks for the underscore underscored add method in the String class and this
add method which is defined in the String class gives the procedure to concatenate strings.
Same is the case when we want to concatenate to list using the plus operator
so these two lives are instances or copies of the built in class list.
And when we want to contact an aide using the plus operator Python find for this method at in the list
class and the method the ad method defined in this list class gives the procedure to concatenate to
list
so depending upon the input parameters the definition of the add method which is a special method or
a magic method is changing.
Essentially each Burton function or operator has a special method or a magic method corresponding to
it.
For example we have the underscored underscored Len underscored underscored corresponding to the length
function and underscored underscored and underscored underscored with just corresponding to the plus
operator every class in Python defined its own behavior for building functions and methods.
And when we pass an instance of some class to a built in function or use an operator on the instance
like we did here it doesn't actually equal into calling a special method.
We did elegant argument say for example we have a built in function
if and C and the corresponding special method for the function is underscored underscore function underscored
under school and Python interpret a call to the function as
object dart underscored underscored function underscored underscore in the case of operators.
If we have an operator like the plus operator say we have the operator or be in the corresponding method
is underscored underscored or be odd underscored underscore and Biden interpret something like this
object 1 dart underscored underscored or Pete I'd underscore underscore and object to its past as an
object
so when we are adding two numbers two and three using the plus operator this actually gets translated
in to
to DOD underscored underscored and underscored underscored.
And the second number three is parsed as an argument and say for example if we want to if we want to
calculate the length or the string hello this gets translated to Hello dart underscored underscore Lynn
underscored underscore and these methods the double underscored method are also called special methods
in Python and the special method at a set of predefined method which we can use to enrich our classes
user defined classes so we can define these special methods or the standard methods in our own classes
we can override the behavior or the function or an operator associated with them because behind the
scenes python is calling up a method.
So let us see how we can use the Dando method let's define two variables x and y.
Now we can use the plus operator in order to add these two integers.
And this is the result.
We can also call the add method the double underscored add method which is defined in the integer class
needed to perform the addition operation.
But on these two objects.
So let's do that.
So when we call the double underscored add method this is the object 1.
And on this object we're calling the add method and the second object is parsed as an argument.
So you have the output next let's concatenate.
Let's concatenate two strings so we will call the add method on this variable e
so here we have these two strings concatenated using the add method when we use the plus operator to
perform addition on two integers or concatenate two strings in the background.
Python is actually making a call to the special method and so polymorphism refers to the ability of
an object to a daddy go to the type of data it this processing.
Next is operator overload.
So what does operator overloading Python makes it possible to change the definition of the building
operators when they're applied to user defined types.
And this is called operator overloading the plus operator in these examples has different meaning depending
on the types of the parameters involved.
And if python does not recognize the operation and that combination of types we get an error Python
provides a set of operators that can be overloaded the plus operator is overloaded.
That means the operator can do odd mean different things depending on the types involved the case if
the input parameters are integers it performs the addition operation if the input parameters strings
it concatenate the plus operator concatenate these the strings if the input parameters are list it concatenate
both the list into a single list and byte and provide the set of operators that can be overlooked.
So these are some of the operators that can be overloaded.
That means they perform different operations or mean different things depending on the types input types
involved so we have the code multiplication operator.
The subtraction operator code and so on and this is the magic method or the special method corresponding
to the operators.
So we have seen the plus operator can be used to add integers and then concatenate strings and also
list so we have the plus operator that is used to concatenate Delius.
Now we will define a user defined class and then see if we can use the plus operator to concatenate
or add to instances of that class
so here you have two instances of the indigent object X and Y.
And we have a plus operator not to perform addition operation on these two instances of the integer
class a and b add to instances of the string class and we can use the plus operator to concatenate these
strings and here we have two instances of the list class and using the plus operator we have concatenated
these two strings.
I'm not going to define a class and see if we can use the plus operator on the instances of the class.
This is the class got and it has an init method not to initialize the instance variables.
This class basically maintains the items ordered by a customer in a list so we will create two instances
of this class got one and got 2 and then use a plus operator in order to contaminate the two instances
so the items in in these two cards need to be concatenated into a single list.
So now let's create two cards two instances of the class card let's say the instances card one.
And the argument that I'm going to pass to the class card is a list so I'm going to pass the element
light and then lamp switches.
So in this God I have three elements light lamp and switches.
So this is got one.
And in another card.
Got two.
I'm going to pass the elements but and then Wyatt so these other two cards.
Now I want to concatenate the elements in both these cards.
So can we perform the addition operation on these two objects.
But before performing the addition operation using the plus operator on these two objects on these two
instances let us check the type or these two instances.
Card 1.
So this is of the type the class card.
Same is the case with the second card.
Card 2.
Now I want to perform or use the plus operator on these two instances.
And we have a type error here.
It says unsupported operant type for the plus operator so this basically means Python does not recognize
the operation.
And this combination of types that is the type cut that does because we have not defined a method in
our class card in order to perform addition operation we do not have a special or the magic method at
which defined these procedure to add to instances of this class card.
So letters include the add method in this class we have now included the add method the special method
in our card class the add method takes in to parameters self and other.
The first parameter is the object on which the add method is invoked.
The second parameter is named either to distinguish it from sale.
So in our example self refers to card 1 and either refers to card to now coming to the body of the add
method we have a return statement and this return statement returns discard object and before that we
are contaminating the items in card one and card to.
Now when we perform a mission operation on these two instances let us see that let us use the plus operator
and on card 1 and card 2 and assign this 2 Card 3.
Now when we print the card 3.
So here we have the elements in both the cards got one and card to concatenated.
That is because we now had an add method the special method and that has given a procedure to concatenate
the instances got one and got two.
So we have just overloaded the plus operator by giving it a new definition and numbered.
Class got.