Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Object.clone()

  1. #1
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Object.clone()

    Hi all,

    Just learning java and have a question about Object's clone method. According to Suns Java tutorial, your class has to implement the Cloneable interface in order to use Object's clone method. If not, an exception will be thrown.

    This seems a bit confusing. If Object has its own implementation of the clone method, and any class created implicitly extends Object, then why does your class need to implement the Cloneable interface in order to use it?

    And if it DOES implement the Cloneable interface, doesn't this mean by definition that the class MUST implement all methods declared in the interface?

    Could someone please clear this up and provide an example?

    Thank you

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,372
    Thanks
    183
    Thanked 836 Times in 779 Posts
    Blog Entries
    5

    Default Re: Object.clone()

    This interface, amongst others such as Serializeable, is called a Marker interface, and is a design pattern used in computer science. There are quite a few definitions of this online, I'd recommend googling Marker Interface or Marker Interface Pattern to get some thorough definitions.

    PS. Please do not cross-post the same thread to the forums, posting once should be sufficient to get your question out there (I have removed your other post).
    Last edited by copeg; July 13th, 2010 at 07:36 PM.

  3. #3
    Member
    Join Date
    Jul 2010
    Posts
    45
    Thanks
    10
    Thanked 3 Times in 3 Posts

    Default Re: Object.clone()

    Sorry for double posting, I was planning on deleting my original post because I thought this forum would be a better place for my question but I could not find a delete option.

    Thanks for the quick response. Based on what I found, it seems that the empty interface is used to flag classes. They are checked with instanceof to see if they implement the empty interface and take action accordingly.

    I can't think of a reason why you would want to do this, but I understand the syntax rules behind it now.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Object.clone()

    Technically you don't have to use the Cloneable interface since the clone method is provided by the object class so the design of the class isn't changed really.

    However, if you want to use that class with polymorphism, say you have an object that you want to only hold objects that are Cloneable, then you would use that marker interface to ensure that the clone method is going to work properly (well, I'm using the abstraction principle where the abstracted method will work perfectly as intended, this is not always the case because someone had to write that method to begin with).

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Object.clone()

    As from the API.

    * The method <tt>clone</tt> for class <tt>Object</tt> performs a
    * specific cloning operation. First, if the class of this object does
    * not implement the interface <tt>Cloneable</tt>, then a
    * <tt>CloneNotSupportedException</tt> is thrown. Note that all arrays
    * are considered to implement the interface <tt>Cloneable</tt>.
    * Otherwise, this method creates a new instance of the class of this
    * object and initializes all its fields with exactly the contents of
    * the corresponding fields of this object, as if by assignment; the
    * contents of the fields are not themselves cloned. Thus, this method
    * performs a "shallow copy" of this object, not a "deep copy" operation.
    * <p>
    * The class <tt>Object</tt> does not itself implement the interface
    * <tt>Cloneable</tt>, so calling the <tt>clone</tt> method on an object
    * whose class is <tt>Object</tt> will result in throwing an
    * exception at run time.
    // Json

  6. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,707
    Thanks
    25
    Thanked 338 Times in 313 Posts

    Default Re: Object.clone()

    This seems a bit confusing. If Object has its own implementation of the clone method, and any class created implicitly extends Object, then why does your class need to implement the Cloneable interface in order to use it?
    The thing that always confused me most about the Object class is the fact that the toString() method returns a String, which extends Object. It seems to be the chicken and the egg situation...

Similar Threads

  1. write object to sql db
    By wolfgar in forum JDBC & Databases
    Replies: 3
    Last Post: May 18th, 2010, 11:03 AM
  2. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 03:33 PM
  3. Help with object
    By Spangle1187 in forum Collections and Generics
    Replies: 3
    Last Post: April 22nd, 2010, 05:34 PM
  4. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  5. Writing clone() method for LinkedList
    By vluong in forum Collections and Generics
    Replies: 6
    Last Post: October 27th, 2009, 09:41 AM