1 00:00:00,000 --> 00:00:02,520 Awesome. We're almost done with 2 00:00:02,520 --> 00:00:05,520 classes. But there's one last concept 3 00:00:05,520 --> 00:00:08,160 that I need you to know, and that is 4 00:00:08,190 --> 00:00:11,520 inheritance. Inheritance is the process 5 00:00:11,580 --> 00:00:15,570 of creating a new class out of a base 6 00:00:15,570 --> 00:00:19,170 class. So a new class that has all the 7 00:00:19,170 --> 00:00:21,600 properties and the methods of a base 8 00:00:21,600 --> 00:00:23,460 class, but also it has some other 9 00:00:23,460 --> 00:00:26,250 methods. So why do we do that? Well, 10 00:00:26,610 --> 00:00:29,040 I'll explain that to you through our 11 00:00:29,040 --> 00:00:32,460 example. So here, we have this 12 00:00:32,735 --> 00:00:35,855 blueprint, so this class, and let me 13 00:00:35,880 --> 00:00:38,130 delete this not to confuse you. So 14 00:00:38,130 --> 00:00:40,080 there, we were just calling the class 15 00:00:40,080 --> 00:00:42,990 instances. So we have this class, which 16 00:00:42,990 --> 00:00:45,300 creates a bank account object for us, 17 00:00:45,764 --> 00:00:48,360 and then we can withdraw and deposit money 18 00:00:48,360 --> 00:00:51,870 from this bank account. Now, what if we 19 00:00:51,870 --> 00:00:54,660 want to transfer money from these bank 20 00:00:54,660 --> 00:00:57,600 account? So from our bank account, 21 00:00:57,600 --> 00:00:59,460 transfer money to another bank account. 22 00:00:59,940 --> 00:01:02,940 Well, we would have to add a transfer 23 00:01:02,965 --> 00:01:04,255 method in here, 24 00:01:04,279 --> 00:01:06,279 [No audio] 25 00:01:06,304 --> 00:01:08,021 and then do something like 26 00:01:08,045 --> 00:01:09,870 just simple like self.balance equal 27 00:01:09,870 --> 00:01:13,200 to self.balance minus the amount, so we 28 00:01:13,200 --> 00:01:15,660 pass in a transfer amount there, and 29 00:01:15,660 --> 00:01:18,600 that is subtracted from our balance 30 00:01:18,600 --> 00:01:23,850 here. But as far as I knew, you cannot 31 00:01:23,940 --> 00:01:25,470 transfer money from your saving 32 00:01:25,500 --> 00:01:27,450 account, you can transfer money from 33 00:01:27,450 --> 00:01:29,250 your checking account, but not from a 34 00:01:29,250 --> 00:01:32,130 saving account. So that method didn't 35 00:01:32,130 --> 00:01:35,160 make sense for a checking account, and 36 00:01:35,160 --> 00:01:37,680 this account is a general bank account. 37 00:01:38,280 --> 00:01:41,430 So what we could do is, maybe we could 38 00:01:41,455 --> 00:01:46,650 create another class, like we could copy 39 00:01:46,650 --> 00:01:48,390 of this and create another class and 40 00:01:48,390 --> 00:01:51,750 call it something else, and so we have all 41 00:01:51,750 --> 00:01:54,690 these methods there. So that's one 42 00:01:54,720 --> 00:01:57,390 solution. The other solution is to use 43 00:01:57,390 --> 00:01:59,550 inheritance. So here is where 44 00:01:59,610 --> 00:02:02,190 inheritance comes in handy. You derive a 45 00:02:02,190 --> 00:02:04,950 class out of a base class, which is a 46 00:02:04,950 --> 00:02:07,650 more general class, like this Account 47 00:02:07,650 --> 00:02:10,710 class in here. So let's create a new 48 00:02:10,710 --> 00:02:13,410 class, and this new class is called, is 49 00:02:13,410 --> 00:02:15,930 referred to as a subclass, and this will 50 00:02:15,930 --> 00:02:20,310 be the base class. So again, you need to 51 00:02:20,310 --> 00:02:24,600 use the same syntax, so you use a class 52 00:02:24,600 --> 00:02:27,000 keyword there to create a class, and let's 53 00:02:27,000 --> 00:02:29,940 call this Checking, so for checking 54 00:02:29,970 --> 00:02:33,690 account. And the first thing we wanted to do 55 00:02:33,690 --> 00:02:37,200 is to create an init method. With that 56 00:02:37,200 --> 00:02:40,080 init method, we will create like this 57 00:02:40,710 --> 00:02:42,810 minimal object that I have been talking 58 00:02:42,810 --> 00:02:46,230 about, like in our base class, we also need, 59 00:02:46,740 --> 00:02:51,420 here we need a balance from our file, and so 60 00:02:51,420 --> 00:02:54,060 we need that filepath. But instead of 61 00:02:54,060 --> 00:02:55,920 writing this code, again, as I said, 62 00:02:56,160 --> 00:02:58,530 what you can do is, well, first you 63 00:02:58,530 --> 00:03:01,290 need to create an init function method, 64 00:03:01,620 --> 00:03:04,050 and of course, pass self in there. So 65 00:03:04,050 --> 00:03:07,830 you always pass a self parameter, 66 00:03:07,855 --> 00:03:11,098 whether that is a base class or a subclass. 67 00:03:11,527 --> 00:03:16,258 And then in this init method, you want to call 68 00:03:16,259 --> 00:03:19,690 [No audio] 69 00:03:19,715 --> 00:03:23,558 the init method of your Account class, 70 00:03:24,371 --> 00:03:26,760 which means that when you create an object instance 71 00:03:26,760 --> 00:03:29,220 of your Checking class, this method will 72 00:03:29,220 --> 00:03:31,950 be executed, and when this method is 73 00:03:31,950 --> 00:03:35,160 executed, this method executes the init 74 00:03:35,160 --> 00:03:37,410 function of your account method so that 75 00:03:37,410 --> 00:03:40,800 you create this minimal object, which 76 00:03:40,800 --> 00:03:43,050 is the same as this one here, but then 77 00:03:43,050 --> 00:03:45,529 we will have some extra methods down here. 78 00:03:47,037 --> 00:03:50,190 So a Checking account is this, plus 79 00:03:50,190 --> 00:03:53,190 some other things, and here, you will need to 80 00:03:53,190 --> 00:03:58,500 pass self and the other parameter that 81 00:03:58,650 --> 00:04:01,380 is necessary for this init function. So 82 00:04:01,380 --> 00:04:02,970 here we are passing two parameters, and 83 00:04:02,970 --> 00:04:07,170 we should do the same in here, and if I 84 00:04:07,200 --> 00:04:10,710 execute this now, I'll get an error. So 85 00:04:10,710 --> 00:04:13,080 by intention, I want to you to see what 86 00:04:13,080 --> 00:04:16,170 error we get. So let me create an 87 00:04:16,170 --> 00:04:18,532 instance of this function. So checking 88 00:04:18,534 --> 00:04:22,258 [Author typing] 89 00:04:22,260 --> 00:04:23,999 and here, let's try that out. 90 00:04:24,007 --> 00:04:26,148 [No audio] 91 00:04:26,150 --> 00:04:27,845 Here, yeah. 92 00:04:27,846 --> 00:04:31,978 [Author typing] 93 00:04:31,980 --> 00:04:33,600 That's inside the account 94 00:04:33,600 --> 00:04:39,121 folder. So here and then acc.py. Let's see. 95 00:04:39,122 --> 00:04:41,896 [No audio] 96 00:04:41,898 --> 00:04:43,920 So the first thing that this is 97 00:04:43,920 --> 00:04:46,440 saying is filepath is not defined in 98 00:04:46,440 --> 00:04:49,350 line 21, which is this one here, and the 99 00:04:49,350 --> 00:04:51,600 reason for that is that we haven't 100 00:04:51,600 --> 00:04:54,840 defined any filepath inside this function. 101 00:04:55,429 --> 00:05:01,470 So you can pass that inside 102 00:05:01,470 --> 00:05:04,560 the init brackets as a parameter, and 103 00:05:04,560 --> 00:05:06,720 then you need to pass that here as well. 104 00:05:07,170 --> 00:05:12,450 So let's say, account\\balance.txt, 105 00:05:13,950 --> 00:05:17,280 and good, and we don't see any output 106 00:05:17,280 --> 00:05:18,810 there because we are not printing 107 00:05:18,810 --> 00:05:21,750 anything out. But what happened 108 00:05:21,750 --> 00:05:24,240 basically is that an object instance was 109 00:05:24,240 --> 00:05:26,880 created, and this was called, this method 110 00:05:26,880 --> 00:05:29,310 was called, and this class was 111 00:05:29,310 --> 00:05:32,160 initialized as well. Now, if we try to 112 00:05:32,160 --> 00:05:35,130 do so the whole point of inheritance is 113 00:05:35,310 --> 00:05:37,680 to do something like checking, so to 114 00:05:37,680 --> 00:05:40,830 access methods, or the base class, 115 00:05:41,190 --> 00:05:43,170 without writing those methods in your 116 00:05:43,170 --> 00:05:48,476 subclass. So let's say checking.deposit, and let's 117 00:05:48,878 --> 00:05:55,277 put an amount there. So 10, now if we execute that, 118 00:05:56,667 --> 00:05:59,040 we get this attribute error, which says 119 00:05:59,070 --> 00:06:00,900 checking object has no attribute 120 00:06:00,900 --> 00:06:03,840 deposits. So this is not recognizing 121 00:06:04,110 --> 00:06:06,960 this method. The reason for that is that 122 00:06:06,960 --> 00:06:10,890 we haven't yet satisfied the syntax of 123 00:06:10,890 --> 00:06:15,150 inheritance. So the key here is to pass 124 00:06:15,180 --> 00:06:18,150 an argument to our checking subclass, 125 00:06:18,330 --> 00:06:20,850 and that argument is the base class. So 126 00:06:20,850 --> 00:06:23,490 the name of the base class, save that if 127 00:06:23,490 --> 00:06:27,150 you do the same now, and maybe print out 128 00:06:27,268 --> 00:06:29,254 the output to see what's happening. 129 00:06:30,460 --> 00:06:32,789 So self.balance. 130 00:06:32,790 --> 00:06:37,475 [No audio] 131 00:06:37,477 --> 00:06:42,218 Sorry, I meant to say checking.balance. 132 00:06:42,219 --> 00:06:46,905 Sorry about that, and yep, now it seems to be working. 133 00:06:46,906 --> 00:06:50,760 So this method was recognized, and what 134 00:06:51,090 --> 00:06:54,960 this did is, from 1100, which was the 135 00:06:54,960 --> 00:07:01,320 initial balance, it went up to 1110. So 136 00:07:01,320 --> 00:07:04,890 this printed out the balance instance 137 00:07:04,890 --> 00:07:08,700 variable of the base class. So that's 138 00:07:08,700 --> 00:07:10,950 how we deposit some money in our 139 00:07:10,950 --> 00:07:12,840 checking account. Now we want to 140 00:07:12,840 --> 00:07:16,320 transfer money. So we need to define a 141 00:07:16,320 --> 00:07:18,993 new method here, let's call this transfer, 142 00:07:18,994 --> 00:07:21,643 [Author typing] 143 00:07:21,645 --> 00:07:24,900 transfer, self there, and also 144 00:07:24,930 --> 00:07:28,050 a local variable amount, so as a 145 00:07:28,050 --> 00:07:31,050 parameter. So this is just a local 146 00:07:31,050 --> 00:07:33,210 variable, it doesn't have to do with the 147 00:07:33,210 --> 00:07:36,060 swans here, and then we want to update 148 00:07:36,060 --> 00:07:38,970 the balance. So we want to transfer some 149 00:07:38,970 --> 00:07:42,330 money to some account, that amount will 150 00:07:42,332 --> 00:07:47,164 be subtracted from our balance. So that means 151 00:07:47,165 --> 00:07:52,404 self.balance equal to self.balance - amount. 152 00:07:53,336 --> 00:07:56,835 Now, if we try that 153 00:07:56,836 --> 00:08:01,739 [Author typing] 154 00:08:01,741 --> 00:08:04,860 new method there. So we are at 155 00:08:04,920 --> 00:08:10,959 this point, let's transfer 110. Yeah. 156 00:08:10,960 --> 00:08:17,398 [Author typing] 157 00:08:17,400 --> 00:08:24,000 And I was expecting to have 1100 here, 158 00:08:24,000 --> 00:08:26,310 but I got this number, and the reason to 159 00:08:26,310 --> 00:08:29,070 that is because, we didn't commit the 160 00:08:29,070 --> 00:08:31,920 changes earlier when we deposited some 161 00:08:31,920 --> 00:08:35,070 money. So when we executed the script, 162 00:08:35,070 --> 00:08:37,680 now, an object instance was created from 163 00:08:37,680 --> 00:08:40,410 the beginning, and it got to this number 164 00:08:40,410 --> 00:08:42,450 here, which wasn't updated to this 165 00:08:42,450 --> 00:08:44,700 because we didn't commit the changes 166 00:08:44,730 --> 00:08:48,570 earlier. But you get the idea. So from 167 00:08:48,570 --> 00:08:51,660 this balance, subtracting this one here, 168 00:08:51,660 --> 00:08:54,120 we get this. Now, that means that the 169 00:08:54,120 --> 00:08:56,880 transfer method is working, but we just 170 00:08:56,880 --> 00:08:59,490 need to apply the commit method there. 171 00:09:00,090 --> 00:09:04,110 So again, we can commit those changes. 172 00:09:05,850 --> 00:09:08,070 Yeah, let's see what we get now. So from 173 00:09:08,070 --> 00:09:12,090 1100, I want to transfer 100, and now 174 00:09:12,090 --> 00:09:16,050 I expect to have this updated to 1000. 175 00:09:17,550 --> 00:09:20,460 Let's see the other 1000 printed out 176 00:09:20,460 --> 00:09:23,610 here, and 1000 in here too. So that is 177 00:09:23,610 --> 00:09:26,670 working great. Also, another thing I 178 00:09:26,670 --> 00:09:30,060 want you to know, about inheritance is 179 00:09:30,060 --> 00:09:33,300 that you can add your own instance variables 180 00:09:33,300 --> 00:09:36,630 for your subclass. Like in this case, 181 00:09:36,660 --> 00:09:39,030 our only instance variable is balance 182 00:09:39,030 --> 00:09:41,280 which has been inherited from the 183 00:09:41,310 --> 00:09:44,370 account base class, but we can add our 184 00:09:44,700 --> 00:09:48,120 own instance variables. So let's suppose 185 00:09:48,630 --> 00:09:51,420 this transfer also comes with some fee. 186 00:09:51,540 --> 00:09:53,430 So when you do a transfer, your bank 187 00:09:53,580 --> 00:09:57,300 charges you some money and you want to 188 00:09:57,330 --> 00:10:00,720 give that fee as a parameter. So let's 189 00:10:00,930 --> 00:10:04,590 keep things simple and say, when you 190 00:10:04,590 --> 00:10:07,980 transfer money, self.balance - amount 191 00:10:08,640 --> 00:10:12,150 minus the fee. So let's now it's apply 192 00:10:12,150 --> 00:10:14,040 percentages there. Let's say, let's 193 00:10:14,040 --> 00:10:16,860 suppose we have a fee of a dollar pair 194 00:10:16,860 --> 00:10:19,800 transfer. Now if we apply the transfer 195 00:10:19,800 --> 00:10:22,560 method again, we'll probably get an 196 00:10:22,560 --> 00:10:25,020 error because this fee is just a local 197 00:10:25,020 --> 00:10:29,386 variable here. So, what you need to apply now 198 00:10:29,387 --> 00:10:33,546 is, you need to transform this fee to 199 00:10:35,452 --> 00:10:41,615 an instance variable, which is self.fee equals to fee. 200 00:10:43,381 --> 00:10:46,953 That means if you pass now a fee here, so let's say 1, 201 00:10:47,340 --> 00:10:51,330 $1 or any other currency, and you 202 00:10:51,420 --> 00:10:54,630 pass it here, so this will go here, and 203 00:10:54,630 --> 00:10:57,540 that we will go here and this will be 204 00:10:58,140 --> 00:11:00,930 equal to the fee that you will passing 205 00:11:00,930 --> 00:11:04,470 here, and then we can make use of this 206 00:11:04,500 --> 00:11:07,560 instance variable and use it in this 207 00:11:07,620 --> 00:11:11,160 transfer method. Great, now we are at 208 00:11:11,160 --> 00:11:15,330 1000 there, and let me execute this. 209 00:11:15,331 --> 00:11:19,258 [Author typing] 210 00:11:19,260 --> 00:11:23,550 Yep, so 899, and yeah, the same here, 211 00:11:24,270 --> 00:11:28,230 which means 1000 minus 100 minus the 212 00:11:28,230 --> 00:11:31,500 fee, you get this number, and yeah, I 213 00:11:31,500 --> 00:11:34,260 hope you are able to see how inheritance 214 00:11:34,260 --> 00:11:36,720 comes in handy to create subclasses out 215 00:11:36,720 --> 00:11:40,620 of a base class. That's it for this 216 00:11:40,620 --> 00:11:42,690 lecture, and we have one more lecture to 217 00:11:42,690 --> 00:11:44,610 go, and in the next lecture, what I'll 218 00:11:44,610 --> 00:11:49,050 do is, I'll clarify some terms of Object 219 00:11:49,050 --> 00:11:51,480 Oriented Programming. So that will be a 220 00:11:51,480 --> 00:11:53,880 short lecture, and that will help you 221 00:11:53,880 --> 00:11:55,470 later on when you refer to 222 00:11:55,470 --> 00:11:57,660 documentation, because there are quite a 223 00:11:57,660 --> 00:12:01,800 few concepts out there in OOP, and I 224 00:12:01,800 --> 00:12:04,110 don't want you to get confused with 225 00:12:04,110 --> 00:12:06,360 those. So I think that's an important 226 00:12:06,360 --> 00:12:08,875 lecture. I'll talk to you later.