File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the Object class clone() method

 
simon fletcher
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the API say?

What happened when you tried it?
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it to see what happens?

Try the following:


 
Vijay Vishwa
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 38
1
Eclipse IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In response question of whether I tried it? Yes. What happened? Compile errors.
 
simon fletcher
Ranch Hand
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But when you say same package, that means in this case the java.lang. package. You cannot write your own code in that package.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic