new clone feature

Discussion in 'Java' started by Roedy Green, Feb 5, 2006.

  1. Roedy Green

    Roedy Green Guest

    There is supposed to be a way you can avoid the cast from clone.

    What is the feature called and how do you use it.
    There is apparently more to it that just saying

    public Footprint clone()

    instead of

    public Object clone()
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Feb 5, 2006
    #1
    1. Advertising

  2. Roedy Green

    Stefan Ram Guest

    Roedy Green <> writes:
    >What is the feature called and how do you use it.
    >There is apparently more to it that just saying
    > public Footprint clone()
    >instead of
    >public Object clone()


    »Note that this definition supports covariant returns -
    that is, the specialization of the return type to a
    subtype (but only for reference types).«

    Java Language Specification, Third Edition,
    8.4.5 Method Return Type
    Stefan Ram, Feb 5, 2006
    #2
    1. Advertising

  3. Roedy Green

    Paul Tomblin Guest

    In a previous article, Roedy Green <> said:
    > public Footprint clone()


    Maybe it's because I come from a C++ background, but I've always preferred
    to make a copy constructor rather that rely on clone(). So I would write
    Footprint myClone = new Footprint(myOldFootprint);
    rather than
    Footprint myClone = (Footprint)myOldFootprint.clone();


    --
    Paul Tomblin <> http://xcski.com/blogs/pt/
    "If Bill Gates had a dime for every time a Windows box crashed...
    ..... Oh, wait a minute, he already does."
    Paul Tomblin, Feb 5, 2006
    #3
  4. Roedy Green

    Paul Tomblin Guest

    In a previous article, (Paul Tomblin) said:
    >In a previous article, Roedy Green
    ><> said:
    >> public Footprint clone()

    >
    >Maybe it's because I come from a C++ background, but I've always preferred
    >to make a copy constructor rather that rely on clone(). So I would write
    > Footprint myClone = new Footprint(myOldFootprint);
    >rather than
    > Footprint myClone = (Footprint)myOldFootprint.clone();


    I should mention that I'm aware of the usefulness of the clone situation
    where the item you're cloning might be one of several subclasses of the
    "Footprint" and you want the new one to be the right one.

    --
    Paul Tomblin <> http://xcski.com/blogs/pt/
    Usenet should require licenses; licenses that can be revoked.
    -- Abigail
    Paul Tomblin, Feb 5, 2006
    #4
    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. Nimmi Srivastav
    Replies:
    1
    Views:
    691
    John Harrison
    Feb 1, 2004
  2. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    703
    lordy
    Jul 7, 2006
  3. Jordan Rastrick
    Replies:
    22
    Views:
    631
    Oren Tirosh
    Mar 27, 2005
  4. laredotornado
    Replies:
    16
    Views:
    7,715
    Lothar Kimmeringer
    Oct 27, 2009
  5. Replies:
    2
    Views:
    436
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page