Everybody welcome to the next video.
So last video we kind of talked about classes.
Now we're gonna go into in this video is called inheritance.
So this works with classes.
Now what inheritances is similar to how when your parents had you and their parents had them.
There were certain genes that were passed down from the generations.
This is called inheritance or you can think of it as when someone passed away their belongings got inherited
down to the next person.
Now this is important with classes because it allows you to generate a structure where for example if
in this particular case we're going to be going over something like a checking account type of application.
So every checking account whether it's a checking account a savings account all relies on a a an account
of some kind.
So in this particular case we have an account class that holds the information about the name of the
account the amount that's in it.
You can get the amount set the amount withdraw or do or do deposits.
Now that's the function of the account class.
Now the checking account class as an example might have a function called Get account type or any other
operations specific to a checking account while a savings account might have an interest earn an interest
that that gets paid to you every month or every year.
However not all accounts have that the savings account does.
You can see how the base account class provides all of the functionality for what an account needs.
And then you have another class that inherits from that so it has all of its properties all of its functions
all of its variables but it gets to manipulate that further to do something more specific for that for
that particular class.
So what we're gonna do is we're going to start off creating a new file and we're gonna call this
account P Y.
So this is going to be our base account.
Right.
So if we do a class account we've got our base class which we've gone to before.
Def in it which is again our constructor self.
So we're going to take in name of account.
Right now we're being taken as a normal starting amount.
Right.
So if we do self-taught name of account it goes name of account and then self that amount is equal to
amount.
So this gives us our starting amount and name of the account.
Now we need to make a few functions that we want for our base an Account Overall no matter what type
of account it is.
So if we do def set amount right about can't spell either set amount against self is required and then
we have the amount we want to set right.
So we self that amount is equal to now.
So this just allows for the bankers as an example to quickly set an amount to start the application
because they made a deposit or it's the initial opening of the account.
We can to get them out which will be important for the customer right.
And we're not going to pass anything to that.
We're just going to return self-taught.
So you can start seeing how we've were making things very similar to the classes that we've built already.
So next part is we want to get the deposit right pause.
So for that we want an amount that we want to deposit.
So we'll do yourself that amount plus equals amount.
So you remember how we use this operator before.
So we're taking in whatever the current value this is and we're adding the amount we want now for The
withdraw method.
So the death with
with your sorted yourself and then the amount we want to withdraw from now because of this we don't
want for example to somebody to be able to withdraw when it gets past zero.
So we'll do a quick if statement here.
So if so if that amount is less than or equal to zero we're just going to return what that says is rock
return any parameter we're not good return of value we're just going to simply not do anything else
so it will ignore everything that's down here.
So down here we want do self-doubt amount minus equals.
So what this does is we've got a we've got a very very basic class here.
We've set the name into the account and the amount that we want to start the account with.
We have an option to set amount for the banker we can get the amount based on the call what the customer
wants or what the banker wants to see.
And we can deposit money.
There's no restriction there.
We just deposit the money and have more for withdraw.
We have a quick check here to make sure that it's not less than zero or equal to zero.
If it does just returns then doesn't perform that withdraw.
You can you might even be able to just return a String here that says failed.
Whatever the case might be our customers are negative.
And then if that does not evaluate teachers so it's greater than zero then we're going to go ahead and
subtract the amount.
Now in this case we can go in the negative and that's perfectly fine because then we can charge them
a a fee later.
Right so this is our basic count.
This is what all accounts are going to have.
So now the inheritance part comes into play.
We're going to create a new account or a new class called checking account.
You want now our checking account is going to inherit all of this data.
So it's going to inherit the name of the the constructor is going to inherit all of these methods here
and all of the code that goes along with it.
So to do this you create a classic normal checking account.
Now the difference here is inside of these parentheses you're going to provide the account name or the
the class that you want to inherit from so in this case we want it to be kept.
Now another piece of importance here is you need to be able to import the account class so from account
like we've done before and board account.
So again from the file name import the class.
So in this case account.
Now we've got our account.
Now we've got our checking our checking account class to do a quick explanation here.
So if we do if we do our constructor right.
This is nothing that we haven't seen before.
We have a name of account
and then the amount.
Right.
That looks perfectly fine however because we're using a constructor.
We want to call the inherit the inherited classes constructor.
So we want to call this we want this to run inside of this.
The way you do that is you put in the name of the previous account.
So account dot and then method.
Right.
So what you pass here is self because it's still calling the you're calling it manually.
So you have to pass self and then you'll do a name of account and then a up.
So this is gonna do is when we come into here and we create a checking account and we give it the name
of the account and Brandon and the amount is five hundred let's say it's going to take this call this
is set to the name of the account and the amount to are instanced based variables which because we're
passing the instanced copy of this checking account it will then know that this is referring to the
checking account or not and not like a savings account as.
So maybe we we want to set the type of accounts we do self-doubt check let's do type of account and
we'll set that to checking.
Right.
Perfect.
So now we've set up a new instance variable with type of account is checking while we still have access
to all of this.
So we do a quick shot here if we do from checking account or checking account
so like we did with person we can do checking equal to checking account and then we need to pass in
a name.
So Brandon and then we need to do the amount was a five hundred.
So if we see here if we print out checking got get a amount which again is a function inside of the
account class not inside of a checking account we go ahead and run that we can see we have our amount
of 500 so that's how easy it is to do the inheritance.
So we can even take this a step further and say def say get account type.
Right.
So we return self duck type of account.
So we go ahead and go over here instead of getting the amount we get the account type and we'll just
print that we can see here that we now see checking.
Now this is important.
It allows us to work with inheritance.
So for example you can have a if you're working on a video game for example you would have less video
games that example it's a good example if you're familiar with gaming concepts but let's say that we
have this account class is called a creature or.
Yeah.
So creatures.
So a creature.
However when we take a step further than that every creature let's say it has 12 mammals.
Right.
So this is now mammals we have eyes hair whatever.
The standard for four mammal is.
And then let's say checking account is now human.
So now we've got mammal.
So everybody every human is a mammal and then it's whatever data past that point.
Sorry about that then.
Every data passed that point inherits from mammal.
That's kind of how it can work.
You can see it as well.
So for checking account we'll do something we'll do def get stats like we kind of did before what we'll
do here is we will return a String will or do a name
the name of the account and then we'll do the type of account
one and then we'll do balance.
All right everybody wants to know how much money they got.
Well do yourself name self that type of account.
Self-taught.
So you can see how we can access these even though they're built in here because it inherits from that.
So right now if we take we have a checking one right we can do here is we can do checking one dog
withdraw.
We want to withdraw one hundred right.
Then we print checking one dot get stacks.
Right.
So this will print us the amount and everything else.
Oh that's called name of the account
so we can see here name of the name.
The account is Brandon type account of checking the balances.
Now four hundred so if we take that
copy the like.
Thank you.
If we deposit
a thousand and then we run this print again
we will now see that we have fourteen hundred and if we decide to let's say we be fine.
Yeah.
So we can do the same thing again with a whole nother checking account.
So if we create checking to and we call this trio we'll start at fifteen hundred right.
Checking to withdraw a thousand checking to get starts checking too.
We're gonna deposit two thousand so now we can see here we've got the name in the counties Brandon then
we've got our withdraw of one hundred.
Then we print the account against.
Now we've got four hundred and then we deposit a thousand.
So now we are fourteen hundred and we have name Victoria type of checking starts with five hundred.
So that's how inheritance works.
It's important to understand the concept.
You may not find it on a rep teams perspective you're not going to be using it nearly as much unless
it makes sense for the script that you're writing but you tend to write a single class or a few classes
for something just to kind of split the code out.
So it's not just one long python file but when it comes to running quick scripts for testing something
as an example you're going to be finding yourself running it in one particular way such as you know
not having this and just going you know paying whatever.
Right.
That's what you're going to find yourself doing for this.
However inheritance is still important understand.
So when you look at somebody else's code and you see what they're doing you can follow the structure
of how it's working.