Lecture thumbnail 0:00 / 0:00 While the Clonable interface certainly doesn’t work for us, but maybe some interface of our own design

will.

I mean, if you explicitly specify that the interface performs a deep copy, then there is no problem,

is there?

So what we can do is we can define an interface called I prototype of t, i prototype of T, for example,

where T is the object being copied.

And then you can specify a method called deep copy.

So right now we’re not being ambiguous anymore.

We’re explicitly saying that this object supports deep copying because it supports the I prototype of

person.

So here we can implement the missing members.

Notice the API is now nice.

It’s not returning object, it’s returning whatever is being copied.

And here, let me just jump back here.

What’s happening is you can actually perform a proper deep copy.

Unfortunately, we’re still stuck with this idea of having to go through every element in the hierarchy

and actually calling some sort of invocation on every single member.

So essentially, in this case, we would return a new person constructed from and here we would have

to say the names followed by the address.

But the address has to also call the copy like so.

So in this case, the address would also implement the I prototype of address.

And here we would specify the member.

And here, of course, we would return new address, taking the current street name as well as the current

house number.

No, not like this.

Let’s try this again.

Oh, sorry about this house number.

There we go.

So this is how you implement this.

And then of course, instead of doing the copy constructor thing, you would say John Dot and you would

do deep copy and let me just execute.

And as you can see, it’s working correctly.

We have the different house numbers, even though everything is in sync, everything is identical.

So this is great.

But we still have this problem that if you have a deep hierarchy of objects, then this approach of

implementing some interface and having these implementations everywhere and having the appropriate constructor

so that you can actually copy things over, that’s very tedious.

If you have ten different classes involved in this kind of setup, then it’s far too much work and we’re

going to look at how to mitigate this in the next clip.

Play Play Play Stop Play Play Start Play information alert