• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question about the Object class clone() method

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the API say?

What happened when you tried it?
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it to see what happens?

Try the following:


 
Ranch Hand
Posts: 40
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 40
1
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In response question of whether I tried it? Yes. What happened? Compile errors.
 
simon fletcher
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 coderanch. 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
Marshal
Posts: 80645
473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic