Hey, welcome back.
In this video, you're going to learn what inheritance is and how to use it in your programs.
So previously in the previous video, we added this credit card feature.
So now we check if the credit card is validated and then we let the user book the hotel.
Now, sometimes, though, some credit cards have another security layer, so it's not enough to enter
the credit card number and the expiration date, the holder name and the CVC.
But you also need to provide some sort of passwords and sometimes even a username and password through
an additional web page.
However, our program doesn't support those credit cards yet.
So what should we do then?
Should we modify the class credit card?
So maybe add some more methods here or modify by the valid methods?
Well, the answer is no, because when you want to add some new features like we're doing now, we're
adding this new secure credit card feature.
When you want to add more features, you don't want to modify existing classes.
So this is one of the principles of o.P.
What you want to do instead is add more classes.
Of course, it's fine to add methods to a class when you are coding that class, so it was fine to add
a defined pay method in the previous video.
If we wanted to do this in a more real life situation.
But once you have completed the class, once you have completed the feature and then you want to add
more features, then it's good to create new classes.
That's what we're going to do now.
Class secure credit card, right?
Now, the thing is that this secure credit cards would be checked for that username and passwords,
which for them where we have created this CSV file, you can find it attached in the lecture resources.
So we have the cards dot CSV file, which we covered in the previous video, and then we have this additional
CSV data.
So in here we have like the card numbers one, two, three, four.
Actually it's this number.
So that's a number of the cards and that is the password of that court, basically.
So the user will have to enter the password of their card in the program and then we'll check against
these data.
So to check against those data means we need a method that checks the password, but we also need to
validate the expiration holder and CVC values as well.
So what we're talking about here is basically having the same class credit card, but with some extended
capabilities.
This extended capability is the capability to check if the password is correct.
So in that case, we don't want to write that class again like in it and then have define, validate
and then have another define authenticate.
Maybe we don't want to rewrite these methods.
What we do instead is we inherit that credit card class from this class.
To inherit a class.
When you define the class in the first line, you use some parentheses, and then there you enter the
credit card class, the name of the class you want to inherit.
And to now this secure credit card class is known as the child, and this is known as a parent class.
So the parent and the child.
So the child is inheriting all these methods from the parents, but the child will be more advanced
than the parent because the child will have this authenticate method.
In addition, you'll see exactly what I mean when I say the child has inherited these methods.
So first let me implement the authenticate method.
So this again would need access to that card, that security, the CSV file.
So for that we say to F here on top cards, security is equal to pandas that read c v cards underscore
security point C is V and let's load the types as str.
And then we go down here in authenticate and we want to get the passwords from the DF cards security
variable, which is a data frame that lock.
So we get DFF number which should be equal to self.
That number.
I'll explain you what self that number is doing here.
So sell that number and then we extract the password.
Right.
Password is this field.
So we have a number column and the password column as well.
Two columns.
So that is the number.
Column.
That is the password column.
So we are saying oh give me the password value where the value of this number column is equal to that.
Now what is that?
Well, that is this here.
So basically, as I told you, this child class inherits everything from this class.
And that is the first example of inheriting.
And you'll see that this class also inherits the methods.
So not only the instance variables such as this self, that number, but also the methods.
And let's add squeeze here.
To get the actual value for the passwords and not just the series.
Then we say if password is equal to given passwords, which is going to be a parameter of the authenticate
method given password.
Return.
True.
Sorry, this has to be an equal comparison.
Operator.
Else return false.
Although you can omit this and it's it will still work.
So then we can go down here and instead of using the credit card class we use, we use the C, Q credit
card class.
Right?
So now credit card, this variable holds an instance of secure credit cards.
And this secure credit card takes the number as an argument, although we don't have an init methods
here, which gets a number as an argument.
We do have an init method in the parent class.
And since this secure credit card class inherits everything from the credit card class.
That means.
It's also inherits that in its methods.
So we can provide all the parameters that the init method takes in the credit card class.
Right.
And then the validate method also can be applied to this secure credit card instance.
Right.
Which is represented by this variable.
So the validate method is also inherited.
Therefore we can pass the methods and all its arguments.
So everything else remains the same.
And of course, we also need to add the authenticate method call here.
So first we validate the codes and then we add another conditional layer if credit codes that authenticate.
And then we end we enter the passwords.
Actually, it's given passwords.
That's the name of the parameter.
So that is equal to the password, which you can get it from the user using like a variable here with
an input function.
Or if you want to keep it simple, you can just provide the passwords which is here my pass.
So write my pass here as a string.
And then if that turns true, which is the case with the password is equal to the given password, then
we want to book the hotel, ask the user for their name, create a reservation ticket instance, print
out the reservation ticket
and perhaps add a new LS statement here ls print.
Credit card authentication failed.
So that else statement is part of this.
If conditional block and this one here belongs to that.
So be careful with the indentation level.
Right.
And that is paired with that.
So we can run the program now run main.
Perhaps an error.
Yep.
Directory cards security not found.
So I had.
My typo here.
Card security does CSC.
It's not cards, it's card security.
So let's run again.
And to the idea of the hotel.
Now all the hotels have no availability.
So we're going to get hotel is not free.
So let's change one of these to yes save the hotels that see the file run again.
188.
Another error.
Let's see what this is telling us.
So look at the line, which is pointing to your file, not to the other files in our other modules.
So that's our main that's p file.
I click there, it takes me here and this is complaining that number was not found in the data frame.
So a key error is part of PANDAS data frames, errors.
So we take a look at the court security seal we file.
Now here I can see that the column number is actually called number.
So that should be some other issue here.
What I can see is that I didn't use the FS card security here in here, but I use DF.
So DF should be replaced DF card security.
Yeah.
Run again.
188 This hotel.
Press enter into your name, audit.
And yeah, we've got.
The reservation ticket if this given passwords here.
Was not my past, but something else like my past one.
And we run against the code.
So let's change this back to Yes, run main 188 and we're going to get credit card authentication failed.
So the data we have entered in here, which is a password, gave us this problem.
So that's what we expected to have this error so that it's working fine.
And yeah, that is about inheritance.
So inheritance is when you create a new class which inherits from an existing class.
So the new class will have additional methods.
So those additional methods are defined in the new class, but you don't need to define the existing
methods of the parent class.
You just call them just like we did here validate and you also can use.
The variables that are passed to.
The class instance.
So number was part of the credit cards class, but it is also part of secure credit card.
And that's about inheritance.
So thanks a lot for following and I'll see you later.