If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

deep copy

for my 102 class, i'm supposed to make a bunch of classes that represent shapes, with a parent AbstractShape class. within these classes, i'm supposed to create deep copies of certain variables. unfortunately, my teacher is on sick leave until the end of this week so i can't ask him for help but i was wondering if anyone could explain to me when exactly (or what) i'm supposed to be making deep copies of, and how to do so. i've tried googling it, but most of the stuff that comes up is a little beyond my current knowledge of java thanks in advance for any help

I thought I answered this one >.<
Deep copying refers to copying a referenced object as opposed to keeping the reference. Java has a clone() function which performs a shallow copy - copies any primitive and retains the pointers to the object properties. Any change in the original or the copy will effect both of the objects. I would override the clone method by implementing Cloneable interface. Hold on I'll find an example online. This one looks pretty good, they have used clone for shallow copies and created a deepClone method for deep ones: http://www.idevelopment.info/data/Pr...ngExample.java

Hope that helps!

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

thanks for the reply fou-lu! unfortunately, my professor supplied us with a test driver which fails our program unless it complies with the architecture that he "specifies" and upon implementing the Cloneable interface or even adding the method clone() fails the architecture tests as specified in the test driver. are there any other ways to clone?

Yes, you can create a cloning constructor.
The logic is the same as using the clone method, you just create a constructor that takes in an instance of the same object type and copies each value that way. The only differences between this route and clone is that you need to copy from argvOther into this, instead of copying this into this. That and the call would be more like MyObj clone = new MyObj(originalMyObj);.

PHP Code:

header('HTTP/1.1 420 Enhance Your Calm');

Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)