Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Curiosity regarding the object class

 
Rajesh Khan
Ranch Hand
Posts: 230
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And catch the CloneNotSupportedException in your clone() method.
The most basic implementation:
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post real code... the class ends before the main method... do you get compiler error while calling obj.clone()?
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic