Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By JosAH

Thread: Why is clone() not in the Clonable Interface?

  1. #1
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Why is clone() not in the Clonable Interface?

    Hi All,

    I want to understand the reason behind designing cloning in java the way it is. Why is the clone method not in the clonable interface? What use does it serve to have it in the Object class instead? Shallow/Deep cloning could've been done the same way we do it today even if the cloning mechanism was entirely with the clonable interface instead of involving the object class.

    Please suggest.

    Thanks
    Gary

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Because otherwise there wouldn't be the basic shallow cloning provided by Object.clone() available, and we'd have to write our own.
    Or something like that.
    It's this clone() that a basic array uses, since they are Cloneable by default.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by garyiskidding View Post
    Hi All,

    I want to understand the reason behind designing cloning in java the way it is. Why is the clone method not in the clonable interface? What use does it serve to have it in the Object class instead? Shallow/Deep cloning could've been done the same way we do it today even if the cloning mechanism was entirely with the clonable interface instead of involving the object class.

    Please suggest.
    The clone() method in the Object class does the allocation of a new object and a bitwise copying of the source object (shallow copying). It needs to be protected because a user might not want to publish this method to the world. If that method were defined in an interface the method needs to be public. I know, it's a bit of a hassle but this implementation gives the user the most freedom (and needs a bit more coding ;-)

    kind regards,

    Jos
    garyiskidding likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The Clonable interface is just a way of marking a class to say the the class can be cloned. There are some classes that look at an object's type to see if it is clonable. Implementing this interface would say that it was.

  5. #5
    garyiskidding is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default

    Hmm.. i am now convinced with the fact that that the clone() method should be protected and still be able to shallow-clone.

    Thanks
    Everyone.

Similar Threads

  1. clone().?
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 02-11-2011, 02:26 PM
  2. How to clone a JTable?
    By IamKira in forum New To Java
    Replies: 3
    Last Post: 07-09-2009, 05:28 PM
  3. How to clone an Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:46 PM
  4. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 09:47 AM
  5. clone problem
    By feniger in forum New To Java
    Replies: 13
    Last Post: 01-13-2008, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •