This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Question about the Object class clone() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about the Object class clone() method" Watch "Question about the Object class clone() method" New topic
Author

Question about the Object class clone() method

simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Simply put, is it possible to invoke the Object's clone() method, from a subclass of Object, without it being overridden in the class definition? Or is the point of it that it was designed to always be overridden? In other words, is it supposed do anything without being overridden?

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

What does the API say?

What happened when you tried it?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Have you tried it to see what happens?

Try the following:


Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
Object.clone() method is protected. Since all the class are indirectly subclass of Object, It is possible to call the clone method. But the call will fail by throwing CloneNotSupportedException unless the clone method is overridden. The cloning is not supported by default.

Also take example to ArrayList class which override clone method.
The ArrayList.clone() does the shallow copy, which means that the object in Cloned ArrayList references the Objects in the Original ArrayList.
Any change in Cloned ArrayList will be reflected in Original ArrayList and vice versa.


The above sysout returns same reference.

To make the deep copy of subobjects in the class, the class should implement Cloneable interface if deep copy is required.


SCJP 5.0 OCBCD 5.0(Formerly SCBCD)
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Vijay Vishwa wrote:Object.clone() method is protected. Since all the class are indirectly subclass of Object, It is possible to call the clone method. But the call will fail by throwing CloneNotSupportedException unless the clone method is overridden. The cloning is not supported by default.

This is wrong.
CloneNotSupportedException is only thrown if the class does not implement Cloneable.
Vijay Vishwa
Greenhorn

Joined: Nov 21, 2010
Posts: 28
This is wrong.
CloneNotSupportedException is only thrown if the class does not implement Cloneable.

Yes, thats right, that's what i mentioned at the end to implement Cloneable interface.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
In response question of whether I tried it? Yes. What happened? Compile errors.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Just tried Tony Docherty's suggested code and it works. Anything I did similar to this using my own code didn't work. Now that I know it should work I can try to figure out where I went wrong. Thanks.

Also, in respone to Vijay Vishwa's first post about ArrayList, I did notice that myself and assumed it has an overridden version in it's definition. Thanks guys.
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
I'm still stuck on this one error that keeps coming up about clone being a protected method. I define a class then call its constructor from the main method of another class (defined in a second java file). With both classes in the same package, the clone method for the first class gets called (from the second java class's main method). I get the "clone() has protected access in Object" message when compiling. If anyone could pass me a hint as to where I have gone wrong, it would be appreciated.

The code that generates the message is in the second file:
First file called Cloner.java

Second file called CloneTest.java, which is the one that I try to compile and produces the message

Code that works:
In a file called Cloner.java, which compiles and runs successfully

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Vijay Vishwa wrote:
This is wrong.
CloneNotSupportedException is only thrown if the class does not implement Cloneable.

Yes, thats right, that's what i mentioned at the end to implement Cloneable interface.

Sorry if I wasn't clear enough but the bit I was referring to that was wrong was your statement

Vijay Vishwa wrote:But the call will fail by throwing CloneNotSupportedException unless the clone method is overridden.

You do not need to override the clone method at all but you must implement Cloneable.
You only need to override the clone() method if you want clone() to have public visibility or if you want to do something specific when cloning an object.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
simon fletcher wrote:I get the "clone() has protected access in Object" message when compiling. If anyone could pass me a hint as to where I have gone wrong, it would be appreciated.

The default clone() method has protected visibility and so is only available to classes in the same package or those that extend the class. Trying to call clone() on a object from a class in a different package will fail with a compile time warning. The solution is to override clone and set it's visibility to public. Whilst doing this you can take advantage of being able to catch the CloneNotSupportedException and cast the return type to your class type. ie

BTW be careful when using clone() there are a number of potential pitfalls. Check out Josh Bloch on Design
simon fletcher
Ranch Hand

Joined: Aug 04, 2012
Posts: 50
Thankyou Tony. I will check out that link in a few moments.

The code for the two classes that I included (that won't compile) is in the same package. Does protected mean that the class calling the clone method also HAS to be a subclass of the class the clone method is being called on? If that's the case then I have been completely misunderstanding at least that access modifier, which is quite possible). I am altering my code so that one subclasses the other but not having any luck that way either.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2170
    
  47
Does protected mean that the class calling the clone method also HAS to be a subclass of the class the clone method is being called on?

No, if it is in the same package it will also work.
The Java tutorial says "The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
But when you say same package, that means in this case the java.lang. package. You cannot write your own code in that package.
 
 
subject: Question about the Object class clone() method
 
Similar Threads
array is an Object But
problem in clone...
Shallow Copy implementation Question
Why doesn't Java introduce new Cloneable Interface?
clone