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 or for base class, so a new class that has all the
properties on 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 you know to confuse
you so that we were just calling the class synthesis, 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 this bank
account? So from our bank account transfer money to another bank account.
Well, we would have to add a transfer method in here 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 know you cannot transfer money from your savings account you can
transfer money from your checking account, but not from your saving account,
so that method wouldn't make sense for a checking account, and this account is
a general background, so what we could do is maybe we could create another class
like we could copy 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. This new class is called,
it's referred to as a subclass, and this will be the base class,
again you need to use the same syntax, so you use a class keyword there
to create a class. Let's call this checking, so for checking account, and the
first thing we want 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, so we need that
file path, 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 the self parameter whether that is base class,
or a sub class, and then in this init method you want to call the init
methods 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 a list minimal object which is the same as this one here, but
then you have some extra methods down here, so a checking account is this plus
some other things, and here you 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 whatever we get, so let me create an instance of
this function. So checking and yeah, let's try that out.
Here, yeah, that's inside the account folder, so here and then acc.py.
Let's see! So the first thing that this is saying is file path is not defined in
line 21 which is this one here and the reason to that is that we haven't
defined any file path inside this function, so you can pass that inside the
init brackets as a parameter, and then you need to pass it here as well so
let's say account balance dot txt. Good. 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 now 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 of the base class without
writing those methods in your subclass. So let's say checking dot 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 deposit so this is not
recognizing this method and the reason to that is that we haven't yet satisfied
the syntax of inheritance and 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.
Say that if you do the same now and maybe print out the altitudes to see
what's happening, so self dot balance. I'm sorry! I meant to say checking
dot balance. Sorry about that! Yeah, now it seems to be working so this
method was recognized and what this did is from 1 100
which was the initial balance it went up to 1 110.
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. 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 these ones 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 equals to self balance minus amount.
Now if we try that new method there, so we are at this point. Let's transfer
110, yeah, and I was expecting to have one thousand and one hundred 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 balanced 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 1 100 I want to transfer 100 and now I
expect to have this updated to 1 000. Let's see, Yeah, 1 000 printed out here,
and 1,000 a year 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,
so balance minus amount minus the fee, so let's not apply
percentages there. Let's say, let's suppose we have a fee of $1 per 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 dot
fee equals to fee, and that means if you pass now a fee here, so let's say 1, $1 or
any other currency, you pass that here. This will go here, and that will go
here and this will be equal to the fee that you're passing you here.
And then we can make use of this instance variable, and use it in this transfer
method. Great, now we are at 1,000 there and lets me let me execute this.
Yeah, so 899, and yeah, the same here. Which means 1000 minus 100 minus
the fee you get this number, and yeah I hope you were 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 will 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, and yeah, I'll talk to you later.