| Author |
why clone() in Object class not accessable from my class?
|
Chrix Wu
Ranch Hand
Joined: Nov 15, 2009
Posts: 121
|
|
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?
|
** SCJP 5.0 84% **
** SCWCD 1.5 76% **
|
 |
Ireneusz Kordal
Ranch Hand
Joined: Jun 21, 2008
Posts: 423
|
|
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
Joined: Nov 15, 2009
Posts: 121
|
|
OK, i see !!
thank you very much!
|
 |
 |
|
|
subject: why clone() in Object class not accessable from my class?
|
|
|