File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Curiosity regarding the object class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Curiosity regarding the object class" Watch "Curiosity regarding the object class" New topic
Author

Curiosity regarding the object class

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
I read that the object class are the root of all classes.So in this case the object class has a .clone method . Objects such as arraylist etc have the clone method but what about our custom classes are they extended from the object class as well ?? If so why dont the instances of those classes have the .clone method ? We have to impliment the cloneable inteface to get that. Any suggestions?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
clone() method is available to all objects. Try to call the clone() method using your object. You will get an exception if the class does not implement the Cloneable interface.

Read clone()
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

You'll also notice that the clone() method in Object is protected. It does exist in all your custom classes, but it's still protected. The default behaviour is to do a shallow copy, but only (as John said) if the class implements Cloneable.

The clone method is designed a bit strangely (and I suspect the language designers would do it differently if they were starting from scratch again), but if you're using it then the approach is:

- Implement Cloneable
- Override clone(), making it public (making it more accessible is fine, it's only less accessible that the compiler will complain about)
- Just call super.clone() if you're happy with the default behaviour, or add whatever you need if you're not.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

And catch the CloneNotSupportedException in your clone() method.
The most basic implementation:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
John Jai wrote:clone() method is available to all objects. Try to call the clone() method using your object. You will get an exception if the class does not implement the Cloneable interface.

Read clone()


Hmm thats what i thought initially that the clone method is available to all objects . So I tried this

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Please post real code... the class ends before the main method... do you get compiler error while calling obj.clone()?
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Okay acording to the java object class it has the following declaration

It means its protected.

Anyways my code is
import java.io.ObjectInputStream.GetField;




and this is the error i get


I know how to use the clone method using Clonable but what i am curious about is why do we get the "Visibility" error when it says the method is protected ??




Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

protected methods are also visible within the same package - so your comparison method isn't the equivalent. Outside the package they're only accessible to subclasses via inheritance - you can call it from a subclass on the same instance, but not on another instance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
It is also worth looking at what Joshua Bloch says about clone(), in Effective Java (old edition), or you can buy the new edition. There are other ways to copy objects, eg with copy constructors or factory methods.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Oh that got cleared up ... I am sorry I didnt realize that protected memebers are available to derived class instances only if they are in the same package.!!!
Thanks for clearing that up..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Rajesh Khan wrote:. . . protected memebers are available to derived class instances only if they are in the same package.!!! . . .
That is incorrect. You will find what protected means in the Java Language Specification, but it isn’t easy to read.
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Campbell Ritchie wrote:
Rajesh Khan wrote:. . . protected memebers are available to derived class instances only if they are in the same package.!!! . . .
That is incorrect. You will find what protected means in the Java Language Specification, but it isn’t easy to read.


Could you simplify that for me ? I still think my definition is correct.

example

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

Rajesh Khan wrote:Could you simplify that for me ? I still think my definition is correct.

I think your understanding is correct (your example correctly demonstrates the behaviour), but the wording of the sentence Campbell quoted is misleading, which is why he challenged it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Curiosity regarding the object class