• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why clone() in Object class not accessable from my class?

 
Chrix Wu
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
clone() is protected in Object class,

because everything is derived from Object, the clone() should be directly used in every classes i made.

but i can't find it when i am programming in my IDE.

why is that?
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected method - means that this method is visible to this class itself, to members of the same package and to subclasses of this class
- but it is not visible to other classes.
Refer to this link to refresh basics on controlling access to members of a class:
http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

If you want to make clone() method accessible to the whole world, declare it as public in a sublcass,
and implement Cloneable interface, otherwise you will get CloneNotSupportedException when trying to call super.Clone
from the sublcass.
See this for details:
http://download-llnw.oracle.com/javase/6/docs/api/java/lang/Cloneable.html

Look at this example:

This code throws CloneNotSupportedException.
If you uncomment line 2, the code runs fine and prints 'test'.

 
Chrix Wu
Ranch Hand
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, i see !!

thank you very much!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic