We've already established in part one that there's going to be some inheritance involved, more specifically
if you go back to your requirements.
You can see that checking savings and loan share common fields and name in balance.
And it doesn't make any sense to keep defining the same fields over and over.
That's when you're going to find yourself copying and pasting code.
And you know my thoughts on that, never do it.
Instead, every account is going to inherit fields from a parent class, so I'm going to put an account
in Java file inside of the account folder.
All right, in the count class is going to serve as the parent for checking savings and loan.
Once again, going back to the requirements.
The developer is only permitted to create an object of type checking savings and loan.
They are not permitted to create an object of the account class.
So what we can do is make this abstract.
And what it's going to do is basically forbid the developer from creating an object of the account class.
The only purpose of account is to provide inheritance for its children.
And we also know every account needs to inherit a string ID.
Now make sure it's private.
Private string name.
And a private double balance.
All right, now, we can create a constructor for the parent class.
Public accounts.
Actually, you know what?
We can just use the Java code generator extension.
We will generate a constructor using all fields, and we can do the same thing for the copy constructor,
but instead of getting values from a bunch of parameters, we're going to get the values from a source
object, the object that we're copying values from sorry start IDs or start name, sorry start balance.
Now we could do the getters and setters again, just generate them using the extension.
Pretty easy.
All right.
And now that we've set up the parent class, every child is going to inherit the fields and methods
that we defined in our parents.
And so checking is going to inherit the fields and methods from the account class.
So it's going to extend account.
Savings is also going to inherit fields and methods from the account class.
And so will the alone account class extends accounts.
And now we're getting errors because every child needs to have a constructor that synchronizes with
the parents constructor.
So we're going to create a constructor instead of checking public checking.
The constructor is going to receive three parameters a string ID.
String name and double balance.
And we're going to use these parameters to update the fields that our checking object is going to inherit.
So our constructor needs to borrow the super constructor.
Basically, this line of code is calling the constructor inside of the parent class, and that constructor
is going to update every field inside of the current object.
All right now, create a copy, constructor.
I'll make some space here.
Public checking.
That's going to receive a source object checking.
Source.
And once again at this constructor needs to borrow the copy constructor inside of its parent.
This line of code is calling the carpet constructor inside of the parent class.
And it's this constructor that's going to update every field inside of the current object.
OK, now we basically do the exact same thing inside a savings critic instructor, public savings.
And the constructor is going to receive three parameters.
String ID.
String name.
And double balance.
And we're going to use these parameters to update the fields that are receiving the object is going
to inherit.
So our constructor needs to borrow the super constructor.
Nail credit, carpet constructor, public savings and the constructor is going to receive a source object
savings source.
And once again, this constructor needs to borrow the copy constructor inside of its parent super source.
OK.
We'll do the exact same thing inside of loan.
Public loan, though, receives three parameters.
When we create a loan object, it needs an ID, a name and a balance.
And we're going to pass those values into the super constructor, and it's the super constructor that's
going to beat every field inside of the current object.
Same thing for the copy constructor.
And the constructor is going to receive a source object.
And once again, the scarper constructor needs to borrow the constructor inside of its parent, and
it's the parent constructor that's going to update every single value inside of the current object.
And that is wonderful.
Now, every single child class is capable of upgrading the fields that it inherits.
What we're going to do is test our code, simply copy these objects from learn the parts.
All right.
We should get some areas that's fine.
We just have to import the classes.
We can just use auto complete.
And.
Should be get to go.
I'll just add three break points.
And we'll debug the time.
So here we're creating an object of the chugging class, the object inherits three fields balance ID
and name.
Here we're borrowing the parent constructor.
And what it does, is it upbeat every single field inside of the current object?
This the object that was just created.
All right.
And here we're creating an object of the savings class, the savings object inherits three fields balance
ID and name.
And here we're borrowing the parent constructor that's going to update every single field inside of
the current object.
This.
All right, everything is looking good so far.
Error creating an object of the loan class.
The constructor receives three parameters.
String ID, string name, double balance, we're passing these parameters into the parent constructor,
and the parent constructor is going to update every single field inside of the current object.
All right.
And we're left with three account objects checking savings and loan.
This is the beauty of inheritance, we only had to define this constructor and this can't be constructor
once, and we borrowed it inside of every single child class instead of redefining the same thing over
and over.
That's why inheritance promotes code reusability.
All right.
So we just confirmed that each child is using the parents constructor to in its fields.
And if you want feel free to test the copy constructor, I'm just going to move on to the final task,
which is to going back on learning the part.
We need to add this to string method.
And now you might think to add a tutoring method inside of each class, like checking savings and loan.
So here inside checking, you might think to say you might think to just override you string copy over
the format from learning the parts.
Here we can say this stark class.
Start, get simple name.
Super dont get it.
Superdog get name.
Super don't get balance.
And that's fine and all, but think about savings and loan, they're all just open up a tab for savings.
Think about savings and loan.
They share the exact same fields as checking, so you can literally just copy and paste the two string
method for loan and savings.
We can say we can copy this over here.
We should get no errors because it shares the exact same fields and copy this over here.
But you know how I feel about copying and pasting code, if you can avoid it, please do.
Instead of redefining that seem to string method three times, every child can just inherit that method
from its parent.
So you can delete this method here.
Actually, it will delete it in every single child.
And re copy the format inside of account Dogefather.
He will say this.
Get class, die, get simple name.
This start get ID.
They start get name.
And this does get bouts.
We're going to be using that to string in the parent class to get the class name of the current object,
the idea of the current object, the name of the current object and the balance.
This is much better because now every child class automatically inherits this tutoring method.
So back in Maine, we can test this out by just starting off with three print statements.
Six out.
Since it's incessant.
And I'll just print the two string that's inherited by checking.
Savings and loan.
OK.
Move these three break points at three break points right here.
Here we can see our checking object is calling the two string method inside of its parent account.
And to string it returns every field inside of the current object.
Perfect.
We'll skip over the rest and everything works out perfectly.
All right, now, your final task was to quality control, just because you can never assume that the
data being passed in is perfect.
There might be something wrong, and if one of these lines ends up being faulty, you want to throw
some kind of exception to force the caller to fix their code and handle such an event.
So inside the constructor?
We'll check if the ID being passed is null or a blank, if I.D. is equal to no.
Or I'd die is blank.
We'll check if the name is Nola Blank.
Name is equal to no.
Or name die is blank.
And if one of these is true, then we need to throw an illegal argument exception.
Throw new a legal argument exception, invalid params notice that I'm throwing the exception only once
here instead of throwing it three different times inside of each child.
It's much more effective to do it once inside the parent, because every child is eventually going to
just call the parent constructor.
Now we can quality control the Centers on Quality Control said it.
So I'll say if.
I.D. is equal to no.
Or if I'd.
Is blank.
Throw a new illegal argument exception.
Invalid ID.
Same thing inside that name, if name is equal to nil or named is blank.
There are new a legal argument exception, invalid name.
OK, and now if the caller tries to pass on any bad values, our app is going to throw an exception,
forcing them to improve their code.
And that's all for part two.
I'll see you soon.