1
00:00:00,000 --> 00:00:05,549
Awesome, we're almost done with
classes, but there's one last concept

2
00:00:05,549 --> 00:00:11,280
that I need you to know, and that is
inheritance. Inheritance is the process

3
00:00:11,280 --> 00:00:19,140
of creating a new class out or for base
class, so a new class that has all the

4
00:00:19,140 --> 00:00:23,820
properties on the methods of a base
class but also it has some other methods.

5
00:00:23,820 --> 00:00:31,199
So why do we do that? Well I'll explain
that to you through our example, so here

6
00:00:31,199 --> 00:00:37,440
we have this blueprint, so this class and
let me delete this you know to confuse

7
00:00:37,440 --> 00:00:42,660
you so that we were just calling the
class synthesis, so we have this class

8
00:00:42,660 --> 00:00:47,399
which creates a bank account object for
us and then we can withdraw and deposit

9
00:00:47,399 --> 00:00:54,629
money from this bank account. Now what if
we want to transfer money from this bank

10
00:00:54,629 --> 00:00:59,359
account? So from our bank account
transfer money to another bank account.

11
00:00:59,359 --> 00:01:06,930
Well, we would have to add a transfer
method in here and then do something

12
00:01:06,930 --> 00:01:13,140
like just simple like self balance equal
to self balance minus the amount, so we

13
00:01:13,140 --> 00:01:20,850
pass in a transfer amount there and that
is subtracted from our balance here, but

14
00:01:20,850 --> 00:01:26,430
as far as I know you cannot transfer
money from your savings account you can

15
00:01:26,430 --> 00:01:31,020
transfer money from your checking
account, but not from your saving account,

16
00:01:31,020 --> 00:01:36,450
so that method wouldn't make sense for
a checking account, and this account is

17
00:01:36,450 --> 00:01:44,990
a general background, so what we could do
is maybe we could create another class

18
00:01:44,990 --> 00:01:49,229
like we could copy this and create
another class and call it something else,

19
00:01:49,229 --> 00:01:56,189
and so we have all these methods there.
So that's one solution. The other

20
00:01:56,189 --> 00:02:01,110
solution is to use inheritance, so here
is where inheritance comes in handy.

21
00:02:01,110 --> 00:02:07,140
You derive a class out of a base class
which is a more general class like this

22
00:02:07,140 --> 00:02:13,410
account class in here, so let's create a
new class. This new class is called,

23
00:02:13,410 --> 00:02:16,390
it's referred to as a subclass,
and this will be the base class,

24
00:02:16,390 --> 00:02:25,299
again you need to use the same
syntax, so you use a class keyword there

25
00:02:25,299 --> 00:02:31,930
to create a class. Let's call this
checking, so for checking account, and the

26
00:02:31,930 --> 00:02:38,349
first thing we want to do is to create
an init method. With that init method we

27
00:02:38,349 --> 00:02:44,139
will create like this minimal object
that I have been talking about. Like in

28
00:02:44,139 --> 00:02:51,909
our base class we also need here we need
a balance from our file, so we need that

29
00:02:51,909 --> 00:02:57,340
file path, but instead of writing this
code again as I said what you can do is

30
00:02:57,340 --> 00:03:03,099
well first you need to create an init
function method, and of course pass self

31
00:03:03,099 --> 00:03:09,939
in there so you always pass the self
parameter whether that is base class,

32
00:03:09,939 --> 00:03:20,229
or a sub class, and then in this init
method you want to call the init

33
00:03:20,229 --> 00:03:26,169
methods of your account class which
means that when you create an object

34
00:03:26,169 --> 00:03:31,449
instance of your checking class this
method will be executed, and when this

35
00:03:31,449 --> 00:03:36,879
method is executed, this method executes
the init function of your account method

36
00:03:36,879 --> 00:03:42,790
so that you create a list minimal object
which is the same as this one here, but

37
00:03:42,790 --> 00:03:50,079
then you have some extra methods down
here, so a checking account is this plus

38
00:03:50,079 --> 00:03:58,120
some other things, and here you need to
pass self and the other parameter that

39
00:03:58,120 --> 00:04:02,919
is necessary for this init function.
So here we are passing two parameters and

40
00:04:02,919 --> 00:04:10,900
we should do the same in here, and if I
execute this now I'll get an error, so by

41
00:04:10,900 --> 00:04:16,630
intention I want to you to see whatever
we get, so let me create an instance of

42
00:04:16,630 --> 00:04:25,439
this function. So checking and yeah,
let's try that out.

43
00:04:25,439 --> 00:04:37,599
Here, yeah, that's inside the account
folder, so here and then acc.py.

44
00:04:37,599 --> 00:04:46,389
Let's see! So the first thing that this
is saying is file path is not defined in

45
00:04:46,389 --> 00:04:51,490
line 21 which is this one here and the
reason to that is that we haven't

46
00:04:51,490 --> 00:05:01,599
defined any file path inside this
function, so you can pass that inside the

47
00:05:01,599 --> 00:05:07,419
init brackets as a parameter, and then
you need to pass it here as well so

48
00:05:07,419 --> 00:05:17,949
let's say account balance dot txt. Good.
We don't see any output there because we

49
00:05:17,949 --> 00:05:23,620
are not printing anything out, but what
happened basically is that an object

50
00:05:23,620 --> 00:05:28,810
instance was created and this was called,
this method was called now this class

51
00:05:28,810 --> 00:05:35,620
was initialized as well. Now if we try to
do, so the whole point of inheritance is

52
00:05:35,620 --> 00:05:41,560
to do something like checking so to access
methods of the base class without

53
00:05:41,560 --> 00:05:47,219
writing those methods in your subclass.
So let's say checking dot deposit and

54
00:05:47,219 --> 00:05:58,000
let's put an amount there, so 10. Now if
we execute that we get this attribute

55
00:05:58,000 --> 00:06:02,949
error which says checking object has no
attribute deposit so this is not

56
00:06:02,949 --> 00:06:09,219
recognizing this method and the reason
to that is that we haven't yet satisfied

57
00:06:09,219 --> 00:06:16,409
the syntax of inheritance and so the key
here is to pass an argument to our

58
00:06:16,409 --> 00:06:21,759
checking subclass and that argument is
the base class, so the name of the base class.

59
00:06:21,759 --> 00:06:28,479
Say that if you do the same now
and maybe print out the altitudes to see

60
00:06:28,479 --> 00:06:34,650
what's happening, so self dot balance.
I'm sorry! I meant to say checking

61
00:06:41,110 --> 00:06:47,770
dot balance. Sorry about that!
Yeah, now it seems to be working so this

62
00:06:47,770 --> 00:06:53,830
method was recognized and what this did
is from 1 100

63
00:06:53,830 --> 00:06:59,530
which was the initial balance it
went up to 1 110.

64
00:06:59,530 --> 00:07:04,870
So this printed out the balance instance

65
00:07:04,870 --> 00:07:11,199
variable of the base class, so that's how
we deposit some money in our checking

66
00:07:11,199 --> 00:07:17,020
account. Now we want to transfer money,
so we need to define a new method here.

67
00:07:17,020 --> 00:07:27,130
Let's call this transfer. Transfer self
there, and also a local variable amount

68
00:07:27,130 --> 00:07:32,440
so as a parameter. So this is just a
local variable it doesn't have to do

69
00:07:32,440 --> 00:07:38,229
with these ones here, and then we want to
update the balance, so we want to

70
00:07:38,229 --> 00:07:43,659
transfer some money to some account.
That amount will be subtracted from our

71
00:07:43,659 --> 00:07:54,520
balance, so that means self balance
equals to self balance minus amount.

72
00:07:54,520 --> 00:08:08,699
Now if we try that new method there,
so we are at this point. Let's transfer

73
00:08:08,699 --> 00:08:24,490
110, yeah, and I was expecting to have one
thousand and one hundred here but I got

74
00:08:24,490 --> 00:08:29,789
this number and the reason to that is
because we didn't commit the changes

75
00:08:29,789 --> 00:08:36,130
earlier when we deposited some money so
when we executed the script now an

76
00:08:36,130 --> 00:08:40,570
object instance was created from the
beginning and it got to this number here

77
00:08:40,570 --> 00:08:45,580
which wasn't updated to this because we
didn't commit the changes earlier.

78
00:08:45,580 --> 00:08:51,820
But you get the idea, so from this
balanced subtracting this one here we

79
00:08:51,820 --> 00:08:56,830
get this now. That means that the
transfer method is working, but we just

80
00:08:56,830 --> 00:09:05,010
need to apply the commit method there.
So again we can commit those changes.

81
00:09:05,010 --> 00:09:12,040
Yeah, let's see what we get now, so from
1 100 I want to transfer 100 and now I

82
00:09:12,040 --> 00:09:20,410
expect to have this updated to 1 000.
Let's see, Yeah, 1 000 printed out here,

83
00:09:20,410 --> 00:09:26,920
and 1,000 a year too, so that is
working great. Also another thing I want

84
00:09:26,920 --> 00:09:33,220
you to know about inheritance is that
you can add your own instance variables

85
00:09:33,220 --> 00:09:39,220
for your subclass. Like in this case our
only instance variable is balance which

86
00:09:39,220 --> 00:09:45,550
has been inherited from the account base
class, but we can add our own instance

87
00:09:45,550 --> 00:09:52,030
variables, so let's suppose this transfer
also comes with some fee. So when you do

88
00:09:52,030 --> 00:09:58,390
a transfer your bank charges you some money.
And you want to give that fee as a parameter,

89
00:09:58,390 --> 00:10:05,950
so let's keep things simple
and say when you transfer money,

90
00:10:05,950 --> 00:10:12,010
so balance minus amount
minus the fee, so let's not apply

91
00:10:12,010 --> 00:10:16,690
percentages there. Let's say, let's
suppose we have a fee of $1 per transfer.

92
00:10:16,690 --> 00:10:22,750
Now if we apply the transfer method
again we'll probably get an error

93
00:10:22,750 --> 00:10:28,870
because this fee is just a local
variable here, so what you need to apply

94
00:10:28,870 --> 00:10:38,440
now is you need to transform this fee to
an instance variable which is self dot

95
00:10:38,440 --> 00:10:48,280
fee equals to fee, and that means if you
pass now a fee here, so let's say 1, $1 or

96
00:10:48,280 --> 00:10:55,510
any other currency, you pass that here.
This will go here, and that will go

97
00:10:55,510 --> 00:11:01,360
here and this will be equal
to the fee that you're passing you here.

98
00:11:01,360 --> 00:11:08,080
And then we can make use of this instance
variable, and use it in this transfer

99
00:11:08,080 --> 00:11:17,130
method. Great, now we are at 1,000
there and lets me let me execute this.

100
00:11:18,480 --> 00:11:27,790
Yeah, so 899, and yeah, the same here.
Which means 1000 minus 100 minus

101
00:11:27,790 --> 00:11:33,250
the fee you get this number, and yeah
I hope you were able to see how

102
00:11:33,250 --> 00:11:39,610
inheritance comes in handy to create
subclasses out of a base class. That's it

103
00:11:39,610 --> 00:11:44,110
for this lecture, and we have one more
lecture to go, and in the next lecture

104
00:11:44,110 --> 00:11:50,560
what I'll do is I will clarify some
terms of object-oriented programming, so

105
00:11:50,560 --> 00:11:55,200
that will be a short lecture and that
will help you later on when you refer to

106
00:11:55,200 --> 00:12:01,810
documentation because there are quite a
few concepts out there in OOP and I

107
00:12:01,810 --> 00:12:06,339
don't want you to get confused with
those, so I think that's an important

108
00:12:06,339 --> 00:12:07,230
lecture, and yeah, I'll talk to you later.

