In this video, we'll create the bank class, so we'll start by setting up inside Bank Java and the
bank is going to be in charge of managing accounts and transactions.
So here I'll create an array list.
Type accounts called accounts, and I'll create another ArrayList.
Not a ray array list of type transaction.
Called transactions.
Now we'll add a constructor that receives no parameters, public bank.
And here we can initialize the area list.
This accounts is equal to a new object of the ArrayList class.
That can store account objects.
Seems like going after import accounts, OK?
Then we'll set this that transactions equal to a new object of the ArrayList class that can store transaction
objects.
OK, now we need methods that we can use to add account objects to the account surrealist and transaction
object to the transactions ArrayList.
We're going to start with ad account.
So we'll see public void ad accounts.
This method receives an account object.
And to the Arab, the state accounts, we're going to add a new copy of the account object.
But wait, a second account is an abstract class.
So it doesn't matter if this is a checking savings or loan object.
We can't create a new account object.
And that's why we need a clone method, a clone method.
What it does is it returns a copy of the object that calls it so inside account counter Java.
We're going to force.
We're going to force every child the class to override Clone, so we'll say public abstract account
clone.
OK.
And now back here, we can say this state accounts don't add account cyclone.
And what that's going to do is return a copy of the object that calls it.
So if this object is checking, it's going to call the clone method from the checking class.
If this object is savings, it's going to call the clone method from the savings class.
If this object is loan, it's going to call the clone method wherever it is from the loan class.
And ultimately, Clone will return a copy of that object.
But right now, we still have to override the clone methods and each child.
So we'll start with checking.
I like to define my clone method right after the copy constructor.
So here I'll override Clone.
OK, now inside Clone, we're going to create a new checking object.
And the object we're creating, it's going to get all of its values from the current object that's calling
this method, which would be this.
We'll do the same thing inside savings.
We'll put a clone method right after the copy constructor in here, I will say return new savings.
This.
Put a clone method inside Lone.
Return a new loan object that's going to get all of its values from the current object, that's calling
this method.
All right, now, we're going to test our clone methods, so go back to the article and copy over these
objects inside mean we can remove the following?
We don't need any more.
An ad for break points.
We're getting a bunch of errors.
Not sure why.
Oh, we got to import the account class.
OK.
Let's visualize the runtime.
Here it creates a new object of the chucking class.
And now in this line, Clone is supposed to return a copy of this object.
How does it do it?
Well, let's step inside.
So Clone returns a new checking object passing in the current object as the source the copy values from.
So ultimately, that's going to call the copy constructor.
OK.
So that new object we just created that became the current object and the one that originally called
this method, that is now the source object that we're copying values from.
Here, the copper constructor is updating its ID, its name and its balance.
And now Clone returns a new copy of that object.
Perfect.
Same thing for savings.
This one is pretty simple, we're just creating a new savings object, passing in three values into
the constructor.
Nothing interesting here, but here Savings Cyclone Clone is returning a new copy of the object that
calls it so we'll step inside.
Clone creates a new savings object, passing in the current object as the source the copy values from.
So ultimately, this is calling the copy constructor.
The object we just created becomes the current object, and the object that originally called the clone
method becomes the source object to copy values from.
And here we're copying every value from the source object into the current one.
Clone returns the new object.
And we're good.