clone method call non-private, non-final methods

Discussion in 'Java' started by puzzlecracker, Jul 23, 2008.

  1. I saw it in the Effective Java book, though understanding still eludes
    me (I suspect it goes on par with the constructor's contract). I don't
    see implications whereby clone calls some public method to copy parts
    of the object, resulting in the disaster or the end of the world sort
    of a thing. Can someone illustrate that?


    Thanks
     
    puzzlecracker, Jul 23, 2008
    #1
    1. Advertising

  2. On Jul 23, 9:29 am, Eric Sosman <> wrote:
    > puzzlecracker wrote:
    > > I saw it in the Effective Java book, though understanding still eludes
    > > me (I suspect it goes on par with the constructor's contract). I don't
    > > see implications whereby clone calls some public method to copy parts
    > > of the object, resulting in the disaster or the end of the world sort
    > > of a thing. Can someone illustrate that?

    >
    >      Are you sure you've read the prohibition correctly?  I can
    > think of no reason to avoid calling public methods in a clone()
    > implementation, although there are excellent reasons to avoid
    > calling overridable methods, the same reasons to avoid calling
    > them from constructors.  Are you sure you haven't read "public"
    > where EJ actually says "non-final?"
    >
    > --
    > Eric Sosman
    >


    Yes, he refers to public (In clone chapter, 2nd edition), because, as
    you said, they can be overridable.

    What is the issue with that?
     
    puzzlecracker, Jul 23, 2008
    #2
    1. Advertising

  3. puzzlecracker

    Daniel Pitts Guest

    puzzlecracker wrote:
    > I saw it in the Effective Java book, though understanding still eludes
    > me (I suspect it goes on par with the constructor's contract). I don't
    > see implications whereby clone calls some public method to copy parts
    > of the object, resulting in the disaster or the end of the world sort
    > of a thing. Can someone illustrate that?
    >
    >
    > Thanks


    class MyClonable implements Clonable {
    int cloneCount;

    public void setCloneCount(int cloneCount) {
    this.cloneCount = cloneCount;
    }

    protected MyClonable clone() {
    MyClonable myClone = (MyClonable)super.clone();
    myClone.setCloneCount(value + 1);
    }
    }

    class MyClonableBreakers extends MyClonable {
    public void setCloneCount(int cloneCount) {
    this.cloneCount = 0;
    }
    }


    The contract of clone() that is given by MyClonable is broken by the
    derived class, without touching the clone method.

    This is obviously a contrived example, but it shows what could happen
    (maliciously or accidentally).

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jul 25, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oliver Wong
    Replies:
    14
    Views:
    1,651
    Chris Uppal
    Jun 13, 2006
  2. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    731
    lordy
    Jul 7, 2006
  3. Replies:
    5
    Views:
    532
    Chris Uppal
    Nov 17, 2006
  4. Daniel Finnie
    Replies:
    3
    Views:
    198
    Logan Capaldo
    Dec 16, 2006
  5. Frank Meyer

    Private methods not so private?

    Frank Meyer, Aug 1, 2007, in forum: Ruby
    Replies:
    14
    Views:
    274
Loading...

Share This Page