0

The question I have is pretty noob like so please excuse me for my ignorance as I am a noob.

I came across code some consultants wrote in the company I work for. When I tried delving into the code, I had no idea why a class was implementing some interface called clonable. So I tried to google this clonable mess, and all I see is stuff like "don't use it" or "use copy constructor instead". I don't know what either of those things are. Could someone please elaborate the reasons for when this kind of cloning is actually needed? Why would I clone an object?

I spoke to the ex-consultant and he mentioned this would allow us to chain methods apparently. Like questionSet.dosomething().doAnotherThing().dowth();

public class QuestionSet implements Cloneable {
     ...

    /* (non-Javadoc)
     * @see java.lang.Object#clone()
     */
    @Override
    public QuestionSet clone() {
        return new QuestionSet(this);
    }

    ...
}
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
  • 2
    I think you misunderstood what the ex-consultant said, or that is the reason he is an ex-consultant – Scary Wombat Dec 25 '13 at 07:35
  • I've never used either of them (except when experimenting), in 17 years of Java. – user207421 Dec 25 '13 at 07:58
  • The key reason as to why `clone()` exists on `Object` _at all_ is to do with arrays: you can clone an array to make a shallow copy. (The exact relationship between arrays and the normal class hierarchy is one of the things that wasn't as smooth as it should have been in Java…) – Donal Fellows Dec 25 '13 at 08:08
  • This is not a duplicate question. I'm asking why cloning an object would be needed. In what circumstances is cloning the object useful. I have not received an answer to that. I was told that it allows for chaining methods together. But I have no idea what that means. NOTE: emphasis on the word NOOB.. – Horse Voice Dec 25 '13 at 16:51
  • @DonalFellows: I don't see what problem arrays would have posed if a base type `CloneableObject` had a protected member `memberwiseClone` which used internal magic to produce a new object bitwise-copied from the old one, and there were a `Cloneable` interface which included a member `Clone`, which arrays could implement? The only correct way for an unsealed type to be cloned properly is for every ancestral type to support cloning, but not all types that will support cloning in derived types will wish to publish that fact and commit derived types to supporting it as well. – supercat Jan 01 '14 at 20:52
  • @supercat Java's arrays are weird types. They're a subclass of object (that effectively implements both `Cloneable` and `Serializable`), but not in a useful way. They also have truly bizarre semantics in places. Thankfully, enums didn't make the same mistakes. – Donal Fellows Jan 01 '14 at 22:01

1 Answers1

1

It is used in situations where you pass an object to some other party. If the object is mutable and you do not want to risk that the other party changes your object you could give it just a clone or copy of your object instead of the original.

Henry
  • 42,982
  • 7
  • 68
  • 84