Awesome. We're almost done with
classes. But there's one last concept
that I need you to know, and that is
inheritance. Inheritance is the process
of creating a new class out of a base
class. So a new class that has all the
properties and the methods of a base
class, but also it has some other
methods. So why do we do that? Well,
I'll explain that to you through our
example. So here, we have this
blueprint, so this class, and let me
delete this not to confuse you. So
there, we were just calling the class
instances. So we have this class, which
creates a bank account object for us,
and then we can withdraw and deposit money
from this bank account. Now, what if we
want to transfer money from these bank
account? So from our bank account,
transfer money to another bank account.
Well, we would have to add a transfer
method in here,
[No audio]
and then do something like
just simple like self.balance equal
to self.balance minus the amount, so we
pass in a transfer amount there, and
that is subtracted from our balance
here. But as far as I knew, you cannot
transfer money from your saving
account, you can transfer money from
your checking account, but not from a
saving account. So that method didn't
make sense for a checking account, and
this account is a general bank account.
So what we could do is, maybe we could
create another class, like we could copy
of this and create another class and
call it something else, and so we have all
these methods there. So that's one
solution. The other solution is to use
inheritance. So here is where
inheritance comes in handy. You derive a
class out of a base class, which is a
more general class, like this Account
class in here. So let's create a new
class, and this new class is called, is
referred to as a subclass, and this will
be the base class. So again, you need to
use the same syntax, so you use a class
keyword there to create a class, and let's
call this Checking, so for checking
account. And the first thing we wanted to do
is to create an init method. With that
init method, we will create like this
minimal object that I have been talking
about, like in our base class, we also need,
here we need a balance from our file, and so
we need that filepath. But instead of
writing this code, again, as I said,
what you can do is, well, first you
need to create an init function method,
and of course, pass self in there. So
you always pass a self parameter,
whether that is a base class or a subclass.
And then in this init method, you want to call
[No audio]
the init method of your Account class,
which means that when you create an object instance
of your Checking class, this method will
be executed, and when this method is
executed, this method executes the init
function of your account method so that
you create this minimal object, which
is the same as this one here, but then
we will have some extra methods down here.
So a Checking account is this, plus
some other things, and here, you will need to
pass self and the other parameter that
is necessary for this init function. So
here we are passing two parameters, and
we should do the same in here, and if I
execute this now, I'll get an error. So
by intention, I want to you to see what
error we get. So let me create an
instance of this function. So checking
[Author typing]
and here, let's try that out.
[No audio]
Here, yeah.
[Author typing]
That's inside the account
folder. So here and then acc.py. Let's see.
[No audio]
So the first thing that this is
saying is filepath is not defined in
line 21, which is this one here, and the
reason for that is that we haven't
defined any filepath inside this function.
So you can pass that inside
the init brackets as a parameter, and
then you need to pass that here as well.
So let's say, account\\balance.txt,
and good, and we don't see any output
there because we are not printing
anything out. But what happened
basically is that an object instance was
created, and this was called, this method
was called, and this class was
initialized as well. Now, if we try to
do so the whole point of inheritance is
to do something like checking, so to
access methods, or the base class,
without writing those methods in your
subclass. So let's say checking.deposit, and let's
put an amount there. So 10, now if we execute that,
we get this attribute error, which says
checking object has no attribute
deposits. So this is not recognizing
this method. The reason for that is that
we haven't yet satisfied the syntax of
inheritance. So the key here is to pass
an argument to our checking subclass,
and that argument is the base class. So
the name of the base class, save that if
you do the same now, and maybe print out
the output to see what's happening.
So self.balance.
[No audio]
Sorry, I meant to say checking.balance.
Sorry about that, and yep, now it seems to be working.
So this method was recognized, and what
this did is, from 1100, which was the
initial balance, it went up to 1110. So
this printed out the balance instance
variable of the base class. So that's
how we deposit some money in our
checking account. Now we want to
transfer money. So we need to define a
new method here, let's call this transfer,
[Author typing]
transfer, self there, and also
a local variable amount, so as a
parameter. So this is just a local
variable, it doesn't have to do with the
swans here, and then we want to update
the balance. So we want to transfer some
money to some account, that amount will
be subtracted from our balance. So that means
self.balance equal to self.balance - amount.
Now, if we try that
[Author typing]
new method there. So we are at
this point, let's transfer 110. Yeah.
[Author typing]
And I was expecting to have 1100 here,
but I got this number, and the reason to
that is because, we didn't commit the
changes earlier when we deposited some
money. So when we executed the script,
now, an object instance was created from
the beginning, and it got to this number
here, which wasn't updated to this
because we didn't commit the changes
earlier. But you get the idea. So from
this balance, subtracting this one here,
we get this. Now, that means that the
transfer method is working, but we just
need to apply the commit method there.
So again, we can commit those changes.
Yeah, let's see what we get now. So from
1100, I want to transfer 100, and now
I expect to have this updated to 1000.
Let's see the other 1000 printed out
here, and 1000 in here too. So that is
working great. Also, another thing I
want you to know, about inheritance is
that you can add your own instance variables
for your subclass. Like in this case,
our only instance variable is balance
which has been inherited from the
account base class, but we can add our
own instance variables. So let's suppose
this transfer also comes with some fee.
So when you do a transfer, your bank
charges you some money and you want to
give that fee as a parameter. So let's
keep things simple and say, when you
transfer money, self.balance - amount
minus the fee. So let's now it's apply
percentages there. Let's say, let's
suppose we have a fee of a dollar pair
transfer. Now if we apply the transfer
method again, we'll probably get an
error because this fee is just a local
variable here. So, what you need to apply now
is, you need to transform this fee to
an instance variable, which is self.fee equals to fee.
That means if you pass now a fee here, so let's say 1,
$1 or any other currency, and you
pass it here, so this will go here, and
that we will go here and this will be
equal to the fee that you will passing
here, and then we can make use of this
instance variable and use it in this
transfer method. Great, now we are at
1000 there, and let me execute this.
[Author typing]
Yep, so 899, and yeah, the same here,
which means 1000 minus 100 minus the
fee, you get this number, and yeah, I
hope you are able to see how inheritance
comes in handy to create subclasses out
of a base class. That's it for this
lecture, and we have one more lecture to
go, and in the next lecture, what I'll
do is, I'll clarify some terms of Object
Oriented Programming. So that will be a
short lecture, and that will help you
later on when you refer to
documentation, because there are quite a
few concepts out there in OOP, and I
don't want you to get confused with
those. So I think that's an important
lecture. I'll talk to you later.