1 00:00:00,360 --> 00:00:03,810 In this tutorial we will talk about polymorphism 2 00:00:07,440 --> 00:00:10,090 in object oriented programming. 3 00:00:10,140 --> 00:00:19,560 The term polymorphism refers to the ability of an object to attach the cord to the type of the data 4 00:00:19,650 --> 00:00:26,430 it is processing so let's understand polymorphism. 5 00:00:26,430 --> 00:00:28,000 Using example. 6 00:00:29,070 --> 00:00:39,300 Let us add two numbers two and three and I'm using a plus operator to perform the addition operation 7 00:00:39,420 --> 00:00:43,350 the interpreter to Don's file this case. 8 00:00:43,380 --> 00:00:51,300 Now let us concatenate two strings hello and whoa. 9 00:00:51,520 --> 00:01:01,240 And if I want to concatenate two strings I have to use the plus operator again and the interpreter in 10 00:01:01,240 --> 00:01:02,140 this case 11 00:01:04,820 --> 00:01:09,490 then we concatenate using the plus operator. 12 00:01:09,560 --> 00:01:13,370 This is what the interpreter returns. 13 00:01:13,370 --> 00:01:25,480 Now I want to concatenate to lis in order to concatenate I have to use the plus operator again and the 14 00:01:25,480 --> 00:01:28,430 output would be one two three. 15 00:01:28,510 --> 00:01:30,340 And this is the output. 16 00:01:30,340 --> 00:01:42,460 If you observe carefully the actions performed by the plus operator the addition operator the different 17 00:01:42,550 --> 00:01:43,720 input parameters. 18 00:01:44,050 --> 00:01:49,500 So then we pass to indigenous to the plus operator. 19 00:01:49,510 --> 00:01:59,200 It is doing a mathematical operation when we are passing two strings to the operator to the plus operator. 20 00:01:59,240 --> 00:02:02,840 It is concatenated ing both of these strings. 21 00:02:02,840 --> 00:02:10,400 Similarly when we pass to list to the plus operator when we want to continue to list using the plus 22 00:02:10,430 --> 00:02:16,490 operator it is concatenate taking both the list into a single list. 23 00:02:16,690 --> 00:02:25,220 No let us come back to the definition or polymorphism the term polymorphism refers to the ability of 24 00:02:25,360 --> 00:02:37,350 an object to adapt to go to the type of data it is processing and an object may provide different implementations 25 00:02:37,440 --> 00:02:45,690 of one of its methods depending on the type of the input parameters when we want to perform addition 26 00:02:45,990 --> 00:02:59,820 between two integers 2 and 3 We know 2 and 3 are instances of the built an integer class and when we 27 00:02:59,820 --> 00:03:11,210 want to perform this addition operation Python actually looks for a method in the integer class which 28 00:03:11,210 --> 00:03:21,590 is the underscored underscore and underscored underscore and this method defines the procedure for performing 29 00:03:22,190 --> 00:03:33,090 addition operation on numbers similarly when we want to concatenate two strings using the plus operator 30 00:03:34,190 --> 00:03:45,660 we know how low and world instances of these string class which is built in class when we want to concatenate 31 00:03:45,810 --> 00:03:56,430 these two strings Python looks for the underscore underscored add method in the String class and this 32 00:03:56,610 --> 00:04:05,040 add method which is defined in the String class gives the procedure to concatenate strings. 33 00:04:05,040 --> 00:04:12,120 Same is the case when we want to concatenate to list using the plus operator 34 00:04:15,470 --> 00:04:23,720 so these two lives are instances or copies of the built in class list. 35 00:04:23,720 --> 00:04:33,110 And when we want to contact an aide using the plus operator Python find for this method at in the list 36 00:04:33,110 --> 00:04:42,870 class and the method the ad method defined in this list class gives the procedure to concatenate to 37 00:04:42,960 --> 00:04:43,410 list 38 00:04:46,470 --> 00:04:57,360 so depending upon the input parameters the definition of the add method which is a special method or 39 00:04:57,450 --> 00:05:00,650 a magic method is changing. 40 00:05:00,780 --> 00:05:09,210 Essentially each Burton function or operator has a special method or a magic method corresponding to 41 00:05:09,210 --> 00:05:09,870 it. 42 00:05:09,930 --> 00:05:19,950 For example we have the underscored underscored Len underscored underscored corresponding to the length 43 00:05:19,950 --> 00:05:28,590 function and underscored underscored and underscored underscored with just corresponding to the plus 44 00:05:28,710 --> 00:05:37,670 operator every class in Python defined its own behavior for building functions and methods. 45 00:05:37,670 --> 00:05:45,500 And when we pass an instance of some class to a built in function or use an operator on the instance 46 00:05:45,890 --> 00:05:51,710 like we did here it doesn't actually equal into calling a special method. 47 00:05:51,710 --> 00:05:57,330 We did elegant argument say for example we have a built in function 48 00:05:59,950 --> 00:06:12,130 if and C and the corresponding special method for the function is underscored underscore function underscored 49 00:06:12,200 --> 00:06:16,910 under school and Python interpret a call to the function as 50 00:06:19,280 --> 00:06:28,420 object dart underscored underscored function underscored underscore in the case of operators. 51 00:06:28,430 --> 00:06:38,010 If we have an operator like the plus operator say we have the operator or be in the corresponding method 52 00:06:38,070 --> 00:06:47,820 is underscored underscored or be odd underscored underscore and Biden interpret something like this 53 00:06:49,840 --> 00:07:01,490 object 1 dart underscored underscored or Pete I'd underscore underscore and object to its past as an 54 00:07:01,540 --> 00:07:01,800 object 55 00:07:05,340 --> 00:07:15,120 so when we are adding two numbers two and three using the plus operator this actually gets translated 56 00:07:15,210 --> 00:07:15,870 in to 57 00:07:18,840 --> 00:07:25,340 to DOD underscored underscored and underscored underscored. 58 00:07:25,560 --> 00:07:36,540 And the second number three is parsed as an argument and say for example if we want to if we want to 59 00:07:36,540 --> 00:07:49,230 calculate the length or the string hello this gets translated to Hello dart underscored underscore Lynn 60 00:07:50,160 --> 00:07:58,530 underscored underscore and these methods the double underscored method are also called special methods 61 00:07:58,530 --> 00:08:09,490 in Python and the special method at a set of predefined method which we can use to enrich our classes 62 00:08:09,520 --> 00:08:18,010 user defined classes so we can define these special methods or the standard methods in our own classes 63 00:08:18,520 --> 00:08:26,320 we can override the behavior or the function or an operator associated with them because behind the 64 00:08:26,320 --> 00:08:29,540 scenes python is calling up a method. 65 00:08:30,040 --> 00:08:38,240 So let us see how we can use the Dando method let's define two variables x and y. 66 00:08:38,370 --> 00:08:44,970 Now we can use the plus operator in order to add these two integers. 67 00:08:45,400 --> 00:08:47,220 And this is the result. 68 00:08:47,320 --> 00:08:54,280 We can also call the add method the double underscored add method which is defined in the integer class 69 00:08:54,580 --> 00:08:56,910 needed to perform the addition operation. 70 00:08:56,920 --> 00:08:58,900 But on these two objects. 71 00:08:59,080 --> 00:09:00,150 So let's do that. 72 00:09:00,580 --> 00:09:07,880 So when we call the double underscored add method this is the object 1. 73 00:09:08,170 --> 00:09:17,830 And on this object we're calling the add method and the second object is parsed as an argument. 74 00:09:17,830 --> 00:09:22,860 So you have the output next let's concatenate. 75 00:09:22,870 --> 00:09:32,250 Let's concatenate two strings so we will call the add method on this variable e 76 00:09:35,860 --> 00:09:46,680 so here we have these two strings concatenated using the add method when we use the plus operator to 77 00:09:46,680 --> 00:09:53,190 perform addition on two integers or concatenate two strings in the background. 78 00:09:53,190 --> 00:10:01,770 Python is actually making a call to the special method and so polymorphism refers to the ability of 79 00:10:01,890 --> 00:10:07,480 an object to a daddy go to the type of data it this processing. 80 00:10:07,530 --> 00:10:09,950 Next is operator overload. 81 00:10:10,380 --> 00:10:17,250 So what does operator overloading Python makes it possible to change the definition of the building 82 00:10:17,310 --> 00:10:22,000 operators when they're applied to user defined types. 83 00:10:22,020 --> 00:10:33,620 And this is called operator overloading the plus operator in these examples has different meaning depending 84 00:10:33,620 --> 00:10:38,480 on the types of the parameters involved. 85 00:10:38,720 --> 00:10:47,020 And if python does not recognize the operation and that combination of types we get an error Python 86 00:10:47,020 --> 00:10:54,850 provides a set of operators that can be overloaded the plus operator is overloaded. 87 00:10:55,120 --> 00:11:04,870 That means the operator can do odd mean different things depending on the types involved the case if 88 00:11:04,870 --> 00:11:13,210 the input parameters are integers it performs the addition operation if the input parameters strings 89 00:11:13,480 --> 00:11:23,470 it concatenate the plus operator concatenate these the strings if the input parameters are list it concatenate 90 00:11:23,530 --> 00:11:31,850 both the list into a single list and byte and provide the set of operators that can be overlooked. 91 00:11:31,900 --> 00:11:36,850 So these are some of the operators that can be overloaded. 92 00:11:36,970 --> 00:11:45,370 That means they perform different operations or mean different things depending on the types input types 93 00:11:45,490 --> 00:11:51,360 involved so we have the code multiplication operator. 94 00:11:51,740 --> 00:12:02,870 The subtraction operator code and so on and this is the magic method or the special method corresponding 95 00:12:02,870 --> 00:12:04,830 to the operators. 96 00:12:04,910 --> 00:12:14,120 So we have seen the plus operator can be used to add integers and then concatenate strings and also 97 00:12:14,120 --> 00:12:21,180 list so we have the plus operator that is used to concatenate Delius. 98 00:12:21,230 --> 00:12:29,630 Now we will define a user defined class and then see if we can use the plus operator to concatenate 99 00:12:29,720 --> 00:12:32,870 or add to instances of that class 100 00:12:35,710 --> 00:12:41,910 so here you have two instances of the indigent object X and Y. 101 00:12:42,040 --> 00:12:49,930 And we have a plus operator not to perform addition operation on these two instances of the integer 102 00:12:49,960 --> 00:13:00,250 class a and b add to instances of the string class and we can use the plus operator to concatenate these 103 00:13:00,250 --> 00:13:12,410 strings and here we have two instances of the list class and using the plus operator we have concatenated 104 00:13:12,410 --> 00:13:14,580 these two strings. 105 00:13:14,580 --> 00:13:25,000 I'm not going to define a class and see if we can use the plus operator on the instances of the class. 106 00:13:25,160 --> 00:13:32,690 This is the class got and it has an init method not to initialize the instance variables. 107 00:13:32,690 --> 00:13:45,920 This class basically maintains the items ordered by a customer in a list so we will create two instances 108 00:13:46,010 --> 00:13:56,640 of this class got one and got 2 and then use a plus operator in order to contaminate the two instances 109 00:13:56,920 --> 00:14:03,760 so the items in in these two cards need to be concatenated into a single list. 110 00:14:05,320 --> 00:14:15,950 So now let's create two cards two instances of the class card let's say the instances card one. 111 00:14:16,540 --> 00:14:27,160 And the argument that I'm going to pass to the class card is a list so I'm going to pass the element 112 00:14:27,250 --> 00:14:33,370 light and then lamp switches. 113 00:14:33,370 --> 00:14:39,010 So in this God I have three elements light lamp and switches. 114 00:14:39,010 --> 00:14:40,450 So this is got one. 115 00:14:41,350 --> 00:14:43,410 And in another card. 116 00:14:43,900 --> 00:14:45,830 Got two. 117 00:14:45,970 --> 00:14:55,570 I'm going to pass the elements but and then Wyatt so these other two cards. 118 00:14:55,660 --> 00:15:01,780 Now I want to concatenate the elements in both these cards. 119 00:15:03,450 --> 00:15:09,510 So can we perform the addition operation on these two objects. 120 00:15:09,690 --> 00:15:16,920 But before performing the addition operation using the plus operator on these two objects on these two 121 00:15:16,920 --> 00:15:22,880 instances let us check the type or these two instances. 122 00:15:22,950 --> 00:15:25,090 Card 1. 123 00:15:25,220 --> 00:15:28,980 So this is of the type the class card. 124 00:15:29,150 --> 00:15:32,400 Same is the case with the second card. 125 00:15:32,400 --> 00:15:33,900 Card 2. 126 00:15:33,950 --> 00:15:42,870 Now I want to perform or use the plus operator on these two instances. 127 00:15:43,510 --> 00:15:46,500 And we have a type error here. 128 00:15:46,540 --> 00:15:58,720 It says unsupported operant type for the plus operator so this basically means Python does not recognize 129 00:15:58,720 --> 00:16:00,340 the operation. 130 00:16:00,340 --> 00:16:12,080 And this combination of types that is the type cut that does because we have not defined a method in 131 00:16:12,200 --> 00:16:20,360 our class card in order to perform addition operation we do not have a special or the magic method at 132 00:16:20,750 --> 00:16:28,450 which defined these procedure to add to instances of this class card. 133 00:16:28,460 --> 00:16:37,340 So letters include the add method in this class we have now included the add method the special method 134 00:16:37,460 --> 00:16:46,920 in our card class the add method takes in to parameters self and other. 135 00:16:47,280 --> 00:16:52,020 The first parameter is the object on which the add method is invoked. 136 00:16:52,140 --> 00:16:57,880 The second parameter is named either to distinguish it from sale. 137 00:16:58,350 --> 00:17:10,720 So in our example self refers to card 1 and either refers to card to now coming to the body of the add 138 00:17:10,720 --> 00:17:21,960 method we have a return statement and this return statement returns discard object and before that we 139 00:17:22,010 --> 00:17:30,720 are contaminating the items in card one and card to. 140 00:17:30,950 --> 00:17:39,230 Now when we perform a mission operation on these two instances let us see that let us use the plus operator 141 00:17:39,260 --> 00:17:45,880 and on card 1 and card 2 and assign this 2 Card 3. 142 00:17:45,880 --> 00:17:48,520 Now when we print the card 3. 143 00:17:48,800 --> 00:18:01,200 So here we have the elements in both the cards got one and card to concatenated. 144 00:18:01,370 --> 00:18:11,180 That is because we now had an add method the special method and that has given a procedure to concatenate 145 00:18:11,570 --> 00:18:16,070 the instances got one and got two. 146 00:18:16,130 --> 00:18:23,060 So we have just overloaded the plus operator by giving it a new definition and numbered. 147 00:18:23,060 --> 00:18:24,470 Class got.