1 00:00:00,000 --> 00:00:01,600 [no audio] 2 00:00:01,600 --> 00:00:03,300 Friends here we are discussing about 3 00:00:03,300 --> 00:00:05,600 inheritance and polymorphism concept 4 00:00:05,600 --> 00:00:06,900 of OOPS. First 5 00:00:06,900 --> 00:00:08,400 let me go with polymorphism. 6 00:00:08,500 --> 00:00:10,600 See basically we know 'poly' means many. 7 00:00:11,300 --> 00:00:15,000 So in Python polymorphism allows us to define same methods 8 00:00:15,000 --> 00:00:16,200 in different classes. 9 00:00:16,700 --> 00:00:21,100 I mean, you can take same method name in different classes. 10 00:00:21,200 --> 00:00:24,000 Even though if you define same method name in different classes, 11 00:00:24,000 --> 00:00:30,300 there is no overlap, and this process is also known as method 12 00:00:30,300 --> 00:00:35,200 overriding. Simply we are overwriting same method in 13 00:00:35,200 --> 00:00:38,400 different classes. Yes, that is possible, that is allowed in 14 00:00:38,400 --> 00:00:43,000 your Python. And we can also use this polymorphism concept in your 15 00:00:43,000 --> 00:00:44,100 inheritance as well. 16 00:00:44,100 --> 00:00:46,900 We will see that while discussing about inheritance. First 17 00:00:46,900 --> 00:00:49,400 let me explain about your polymorphism with a simple 18 00:00:49,400 --> 00:00:52,400 simple example. Let me open my Sublime Text Editor. 19 00:00:52,700 --> 00:00:54,500 So here I am defining two classes, 20 00:00:54,500 --> 00:00:55,700 let's say 'tomcat', 21 00:00:55,700 --> 00:00:57,910 [no audio] 22 00:00:57,900 --> 00:01:01,000 and I am defining a constructor method here. 23 00:01:01,300 --> 00:01:05,400 The purpose of constructor method is to assign, let's say 24 00:01:05,400 --> 00:01:09,400 'tomcat', 'home' and 'version' by default whenever we are creating 25 00:01:09,400 --> 00:01:12,300 an object. We know initialization method is a constructor method. 26 00:01:12,300 --> 00:01:16,000 It will execute by default whenever if you create an object. 27 00:01:16,900 --> 00:01:19,300 Right. So simply I am writing 'home', 28 00:01:19,600 --> 00:01:24,000 then let's say 'self.version = ver'. 29 00:01:25,500 --> 00:01:27,600 Right. Then finally write, 30 00:01:28,300 --> 00:01:30,400 I'm not expecting any 'return' value from this method, 31 00:01:30,400 --> 00:01:33,200 that's why simply I am writing 'return None'. Then let me write 32 00:01:33,200 --> 00:01:35,400 one more method called 'display'. 33 00:01:35,400 --> 00:01:38,610 [no audio] 34 00:01:38,600 --> 00:01:41,500 So simply I am writing, 35 00:01:42,600 --> 00:01:46,700 "This is from tomcat class", and here 36 00:01:46,700 --> 00:01:48,700 simply I am displaying 'print( 37 00:01:50,300 --> 00:01:53,600 self.home), then 'print( 38 00:01:54,600 --> 00:01:56,900 self.version)'. 39 00:01:57,600 --> 00:01:59,100 That's it. 40 00:01:59,100 --> 00:02:00,100 Then from this function 41 00:02:00,100 --> 00:02:02,900 also, I mean from this method also I'm not expecting any 42 00:02:02,900 --> 00:02:04,800 return value. Simply I'm writing 'return None'. 43 00:02:05,800 --> 00:02:08,199 Let me take same method. 44 00:02:08,199 --> 00:02:11,300 [no audio] 45 00:02:11,300 --> 00:02:14,800 But here I am taking web server class name, 46 00:02:14,800 --> 00:02:16,000 let's say 'Apache'. 47 00:02:16,000 --> 00:02:18,709 [no audio] 48 00:02:18,700 --> 00:02:20,600 Yes, we created two classes. 49 00:02:20,600 --> 00:02:24,500 But if you observe, here I have taken initialization method, 50 00:02:24,500 --> 00:02:27,600 and here also I have taken initialization method. 'display' method 51 00:02:27,600 --> 00:02:28,900 common in both the classes. 52 00:02:29,200 --> 00:02:31,600 No problem. It is allowed in your Python. 53 00:02:32,400 --> 00:02:34,400 It is allowed in your Python. 54 00:02:34,400 --> 00:02:36,100 [no audio] 55 00:02:36,100 --> 00:02:39,100 Right. See if I run this code you're not getting any error. 56 00:02:39,600 --> 00:02:41,900 Anyway, let me create an object from your 'tomcat', 57 00:02:41,900 --> 00:02:44,600 and from your 'Apache'. Let's say 'tom_ob 58 00:02:45,400 --> 00:02:46,800 = Tomcat()'. 59 00:02:47,800 --> 00:02:51,400 So while creating an object you're having initialization 60 00:02:51,400 --> 00:02:53,600 method which is expecting two arguments, 61 00:02:53,600 --> 00:02:57,000 let's say 'home' and 'version'. Then let me pass that. Lets say 62 00:02:57,000 --> 00:02:58,900 '/home/tomcat. 63 00:02:58,900 --> 00:03:00,910 [no audio] 64 00:03:00,900 --> 00:03:04,800 9', and then I am taking suppose 'version' as some 7.6, 65 00:03:04,800 --> 00:03:05,800 something like that. 66 00:03:06,600 --> 00:03:10,800 Then let me create 'apa_ob' also 67 00:03:12,200 --> 00:03:14,000 from your 'Apache' class. 68 00:03:14,800 --> 00:03:17,800 So it is also using initialization method, that means it is 69 00:03:17,800 --> 00:03:19,100 expecting two arguments, 70 00:03:19,100 --> 00:03:21,800 we need to pass them while creating an object. 71 00:03:22,000 --> 00:03:25,400 Let's say 'version' as some 2.4. Let me run this and see the 72 00:03:25,400 --> 00:03:26,700 result. There is no error. 73 00:03:27,300 --> 00:03:28,300 That's fine. 74 00:03:29,000 --> 00:03:32,600 Now I want to display the details of 'tom_ob', and also 75 00:03:32,600 --> 00:03:37,700 display the details of 'apa_ob'. Now in 'tom_ob' 76 00:03:37,700 --> 00:03:40,900 we have 'display' method. If I call this 'display' method 77 00:03:40,900 --> 00:03:42,400 with respect to 'tom_ob' 78 00:03:42,400 --> 00:03:45,300 then it will call from here only so that you are going 79 00:03:45,300 --> 00:03:50,000 to get information for your tomcat object. See guys 80 00:03:50,000 --> 00:03:53,800 simply we define same method name in different classes. No 81 00:03:53,800 --> 00:03:57,800 problem. Even though if we take your Python don't get confused, 82 00:03:57,900 --> 00:03:59,500 it will work perfectly. See the result. 83 00:03:59,500 --> 00:04:01,400 [no audio] 84 00:04:01,400 --> 00:04:02,900 This is from 'Tomcat' class. 85 00:04:02,900 --> 00:04:05,300 [no audio] 86 00:04:05,300 --> 00:04:06,300 Right. 87 00:04:07,700 --> 00:04:10,900 Let me call from 'apa_ob', 'display' method. 88 00:04:11,900 --> 00:04:15,800 Now what you are going to get, see the result. "This is from" 89 00:04:15,800 --> 00:04:17,909 [no audio] 90 00:04:17,800 --> 00:04:20,399 Yeah, sorry. I have written, actually I copied, right, 91 00:04:20,399 --> 00:04:23,700 let me modify here 'apache'. Both the places 92 00:04:23,700 --> 00:04:25,800 actually we have same code. Now, 93 00:04:25,800 --> 00:04:27,000 let me run this and see the result. 94 00:04:28,300 --> 00:04:29,700 Whenever if you are calling 95 00:04:29,700 --> 00:04:33,400 from 'tom_ob', 'display' method it is going to execute your 96 00:04:35,100 --> 00:04:38,200 this 'display' method, right. And whenever if you're calling 97 00:04:38,200 --> 00:04:42,000 from 'apa_ob' it is executing the method which 98 00:04:42,000 --> 00:04:43,700 is there in 'apa_ob'. That's it. 99 00:04:44,600 --> 00:04:47,200 Even though if you override your method in different classes, 100 00:04:47,200 --> 00:04:51,100 your Python won't get confused, and this is simply polymorphism. 101 00:04:51,300 --> 00:04:54,600 Defining same method name in different classes is nothing 102 00:04:54,600 --> 00:04:57,000 but polymorphism, right? 103 00:04:57,300 --> 00:05:00,700 That's fine. Now our next concept is inheritance. 104 00:05:01,600 --> 00:05:05,800 See inheritance is a mechanism that allows us to create new 105 00:05:05,800 --> 00:05:10,300 class known as a child class, and that is based upon an existing 106 00:05:10,300 --> 00:05:12,200 class. That is parent class. 107 00:05:13,000 --> 00:05:16,300 Nothing is there. This concept is very similar to our 108 00:05:16,300 --> 00:05:19,400 [no audio] 109 00:05:19,400 --> 00:05:22,500 like human life. Suppose 110 00:05:23,300 --> 00:05:26,900 we are inheriting genes from our parents. 111 00:05:28,300 --> 00:05:29,800 Right. That is the concept here. 112 00:05:29,800 --> 00:05:32,200 Let me explain this with a script so that you can understand. 113 00:05:33,200 --> 00:05:35,000 Now, let me take one more script. 114 00:05:35,300 --> 00:05:38,800 So here I am saving it as a 'inheritance.py'. 115 00:05:38,800 --> 00:05:41,510 [no audio] 116 00:05:41,500 --> 00:05:45,700 So guys first thing let me take same script only, 117 00:05:45,700 --> 00:05:48,210 [no audio] 118 00:05:48,200 --> 00:05:50,500 and if I open this script here. 119 00:05:51,800 --> 00:05:55,700 Guys, if you observe in these two classes, there are common 120 00:05:55,700 --> 00:05:58,900 methods, at least 'display' method. 121 00:05:58,900 --> 00:06:01,700 [no audio] 122 00:06:01,700 --> 00:06:05,000 See, for 'display' method already it is there in 'Tomcat'. 123 00:06:05,100 --> 00:06:08,800 Why should I write a code here? If you observe entire syntax 124 00:06:08,800 --> 00:06:12,000 is same no, here and here. If it is similar 125 00:06:12,500 --> 00:06:16,100 why should I write same code in the second class? 126 00:06:16,100 --> 00:06:18,710 [no audio] 127 00:06:18,700 --> 00:06:24,200 Right. So in that case, what I can do is I can inherit 'Tomcat' class 128 00:06:24,200 --> 00:06:28,600 in my 'Apache' so that I can use 'display' method for 'apa_ob', 129 00:06:28,600 --> 00:06:31,400 for 'Apache' class. How it is possible? Just observe 130 00:06:31,400 --> 00:06:35,500 that. I am removing 'display' method from your second class. 131 00:06:35,500 --> 00:06:37,510 [no audio] 132 00:06:37,500 --> 00:06:42,000 And if you want to inherit any class, instead of this object 133 00:06:42,000 --> 00:06:43,600 you have to write the particular 134 00:06:45,300 --> 00:06:46,800 class name. That's it. 135 00:06:47,300 --> 00:06:50,500 Now the meaning for this is, you are inheriting these two 136 00:06:50,500 --> 00:06:52,400 methods in this class. 137 00:06:53,400 --> 00:06:55,900 And if you observe same method is there here and here. No 138 00:06:55,900 --> 00:06:58,600 problem. Even though if you are having, first preference is 139 00:06:58,800 --> 00:07:03,300 in this Apache, this one. If it is not there in Apache, then 140 00:07:03,300 --> 00:07:06,000 only it will take this initialization method. That is called 141 00:07:06,000 --> 00:07:09,100 polymorphism. That's fine. 142 00:07:09,900 --> 00:07:12,600 Now see, let me create your two objects. 143 00:07:12,600 --> 00:07:20,610 [no audio] 144 00:07:20,600 --> 00:07:24,300 Right. Now remove this 'print' because you are inheriting this 145 00:07:25,200 --> 00:07:28,100 'Tomcat' into this 'Apache' class. 146 00:07:29,000 --> 00:07:30,500 Now, I created two objects. 147 00:07:30,600 --> 00:07:33,600 It's fine. There is no error. Right. Now 148 00:07:33,600 --> 00:07:37,500 see I am calling your 'tom_ob.display'. 149 00:07:37,500 --> 00:07:40,310 [no audio] 150 00:07:40,300 --> 00:07:41,800 Then you are getting 'Tomcat' details. 151 00:07:42,500 --> 00:07:47,000 If you observe in 'Apache' we don't have, we don't have 'display' 152 00:07:47,000 --> 00:07:51,600 method, but I am trying to do that. 'apa_ob.display'. 153 00:07:51,600 --> 00:07:54,210 [no audio] 154 00:07:54,200 --> 00:07:57,800 You're getting your information. Your 'display' method is executing 155 00:07:57,800 --> 00:08:00,700 even though if you don't have 'display' method here because 156 00:08:01,000 --> 00:08:05,700 we inherited this 'Tomcat' class into 'Apache', so that whatever 157 00:08:05,700 --> 00:08:09,000 the methods are there in this 'Tomcat', all methods are valid 158 00:08:09,000 --> 00:08:13,300 in this 'apa_ob' also. But initialization method is 159 00:08:13,300 --> 00:08:16,600 already there here and here, that's why preference is for this 160 00:08:16,600 --> 00:08:18,300 initialization method, not for this. 161 00:08:18,300 --> 00:08:21,110 [no audio] 162 00:08:21,100 --> 00:08:26,600 Right. So this is the way how we can inherit a method of one class 163 00:08:26,600 --> 00:08:31,000 into other class. What we are doing? Simply while creating 164 00:08:31,000 --> 00:08:32,900 your second class 165 00:08:33,000 --> 00:08:37,000 we are writing in place of parentheses, in place of object 166 00:08:37,000 --> 00:08:40,000 we are writing your required class from which you need to 167 00:08:40,000 --> 00:08:44,200 inherit, from which you need to import the methods, that class 168 00:08:44,200 --> 00:08:47,200 name you have to write so that the methods which are available 169 00:08:47,200 --> 00:08:51,100 in your this class will be available with this object 170 00:08:51,100 --> 00:08:53,799 also, I mean, whatever the object you are going to create 171 00:08:53,799 --> 00:08:55,900 from this class also. That's it. 172 00:08:56,200 --> 00:09:00,200 That is the simple way to understand inheritance concept. 173 00:09:00,300 --> 00:09:04,100 Anyway, you will get more ideas while writing our real-time scripts. 174 00:09:04,900 --> 00:09:09,200 Okay. So finally, what is the advantage by using inheritance? Simple. 175 00:09:10,100 --> 00:09:15,000 This saves you from duplicating a lot of code. Suppose 176 00:09:15,200 --> 00:09:18,500 you are defining same method in different classes and with the 177 00:09:18,500 --> 00:09:21,400 same code. Why should we repeat same code in different classes? 178 00:09:21,600 --> 00:09:25,200 Define in one class and inherit that class into your current 179 00:09:25,200 --> 00:09:27,900 class, so that you can use the previous class methods in the 180 00:09:27,900 --> 00:09:31,600 current class also. That is the advantage. And one more thing. 181 00:09:31,800 --> 00:09:33,200 Yeah, last point. 182 00:09:33,200 --> 00:09:35,310 [no audio] 183 00:09:35,300 --> 00:09:39,500 For 'Apache', we are inheriting methods from 'Tomcat'. Now, here I 184 00:09:39,500 --> 00:09:44,100 can say 'Tomcat' is the parent class and 'Apache' is the child class, 185 00:09:44,600 --> 00:09:48,400 or 'Tomcat' is the superclass and 'Apache' is the subclass. 186 00:09:48,500 --> 00:09:50,200 You need to remember those names. 187 00:09:51,500 --> 00:09:57,100 Parent, 'Tomcat' or superclass is 'Tomcat'. 'Apache' is the child 188 00:09:57,100 --> 00:09:58,300 class or subclass. 189 00:09:59,500 --> 00:10:01,400 Right. That's it guys. 190 00:10:01,500 --> 00:10:03,600 Thank you for watching this simple video. 191 00:10:03,600 --> 00:10:05,763 [no audio]