7.3 Bank Account Part 2
Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
or
Already have an account? Sign In »

Video Transcription
00:00
hi and welcome back to part two of the Abstraction lab. Now, if you remember, we finished off where we left off. By creating our withdrawal method and in this method, we will be taking out a specific amount from our bank account if the balance happens to be greater than zero and then we would return that balance
00:21
if we scroll up the comments that we wrote as or that I wrote for you as a sort of guidance suggest that after curating the withdrawal method, we should have an abstract method to display a customers
00:34
balance.
00:36
Now, I will first go ahead and show you how to create this abstract method. And then we'll dive into the explanation as to why we're making this method abstract. So in order to create this method, I would say first say public and then the keyword abstract.
00:50
And then it is returning a string. So I would write to string, which is the type of the return type,
00:55
and then the name I would like to give this method is display balance
01:00
displayed balance.
01:02
Perfect.
01:03
Now, if you remember, abstract methods do not have a body. So I I'm gonna leave it like this.
01:10
So why are we making this method abstract?
01:12
No.
01:15
The reason I've decided to make this method abstract is because it will have its own unique implementation in the checking account and savings account classes. And since this method is abstract,
01:29
we kind of n forces us to provide the implementation details of this method since we're not providing them in the abstract class itself.
01:38
So when we make our checking account and savings account class, we will have to complete the implementation of this abstract method called display balance. And that is essentially why I've decided to make this method abstract.
01:55
All right, so that is it for our bank account classes. Faras I know. And now we're gonna move on to our checking account class. Now, in our checking account Class A Z can see I've written public costs checking account extends a bank account.
02:10
Now, the reason I said extents bank account is because I would like to have
02:15
thes attributes and methods. I would like to be able to use them in my checking account. Now, this is related to the next module which is on inheritance. So I'm not gonna give too many details out there.
02:28
Okay, So first here, according to or what this commenters are suggesting we should be creating a constructor. Now, as you know, we would first right, public
02:38
checking account. And then I'm going to say string
02:45
new cost name
02:47
and then in new balance.
02:51
And then I'm going to use a super cured to reference, um,
02:57
the new customer name and new balance from my super class, which is the bank account class. So for that, I would say super.
03:07
And then in here, I would write new cost name
03:10
and then super
03:17
new balance.
03:23
Okay.
03:24
All right. So let's see why it is giving us an heir.
03:30
All right, So
03:34
are you. Okay? So it's a string to strain
03:38
and new balance.
03:42
Let's see.
03:46
Okay, So if you go in here, super
03:51
create constructor removed,
03:53
right, there's something. Let's see. Let's see. Creek encircling bank account
04:01
string. New cost name. Okay, let's see.
04:11
So for some reason, this does not seem to be working.
04:14
And let's figure this out together.
04:17
Okay, So the reason why it isn't working and I just remembered is because we're not supposed to write super twice. That was a stupid mistake, so I'll go ahead and change that to this over here. All right. Perfect.
04:32
I'm so turns out you're supposed to add whatever you are trying to reference in the single line with the super cute instead of having two serpents of Rick. You words.
04:43
All right, so sorry about that. But I guess we learned something. You
04:46
okay? So as you can see, they're still in error here. And the Earth says class checking account
04:51
must either be declared abstract or implement the abstract method display balance. And that is exactly what we were talking about in the intro to Abstraction
05:00
video, which is the first video lecture of this model where we said that if a class is abstract than it has to implement, So if a class is extending a abstract class than it has to implement the abstract methods or method in that app, start class. But if a class has an abstract method,
05:19
that doesn't necessarily mean that the class itself
05:23
it is abstract.
05:25
Okay, so we're gonna be doing that, And an easy way to do that is just a click on this air over here and select implement methods. It will show you What method we you need to implement in this case, it's a display balance. So I will go ahead and click. Okay.
05:39
Okay. So as you can see, it is saying at override public string display balance. And now we need to change our return statement too.
05:48
Ah, print out or display the customer's information that is related to their, um, checking account.
05:57
So first, I'm gonna say, get cussed name
05:59
and then plus
06:00
a space, plus your current bonds. So I'm gonna say something like your current
06:09
balance in
06:12
the
06:14
checking
06:15
account.
06:17
I can't.
06:19
It is.
06:24
And then I'm gonna add a pulse over here and get bounce.
06:28
Okay.
06:31
All right. So, uh, sorry. Just a second and remove this one here. Perfect. So what that is gonna do is it's going to the return
06:40
the customers balance for specifically for their checking account.
06:46
All right, so we're gonna be doing something similar for our savings account in which we are going to first create a constructor. So public
06:57
public
07:00
savings, Sorry. Public savings account.
07:05
And then
07:06
And here we're going to have our parameters. So string
07:12
new
07:13
new coast name
07:16
and
07:18
new
07:19
balance.
07:21
And then what? We're gonna do is we're gonna call super on these
07:26
two parameters. So new Coast name and then new balance.
07:31
Okay, Perfect. So that is us creating our constructor. And now we need to again implement the abstract method in our bank account class. Since we are extending the bank of class for the savings account class
07:46
so that we will just do by right clicking on the air and click on by clicking include implement methods and that'll implement our display balance method.
07:55
No, we need to provide a specific implementation for that. And for that, I'm just gonna do the exact same thing that we did over here so we can go ahead and copy this string over here.
08:05
Take it over to this class pasted here
08:09
instead of checking account. I'm just going to write
08:11
savings account
08:13
savings account.
08:15
All right, Perfect. So that has completed our three classes. And in the next s in part three of the sub. We're going to be
08:26
checking out our tester class and seeing how this program actually works. Okay, so thank you very much for watching. And I look forward to seeing you in part three of the slap
Up Next
Similar Content