This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clone method giving error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clone method giving error" Watch "Clone method giving error" New topic
Author

Clone method giving error

deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi all,
I'm not getting why this code is giving error.

class CloneTest1
{
public static void main(String s[])
{
Tree tree = new Tree();
Tree tree1 = (Tree)tree.clone(); //////////!!! this line is
giving error
}
}
class Tree implements Cloneable
{
String name = "name";
}
the error is
CloneTest1.java:7: Can't access protected method clone in class
java.lang.Object
. Tree is not a subclass of the current class.
Tree tree1 = (Tree)tree.clone();
pls. help me out.
regards
deekasha
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
please modify as below : it works !
class CloneTest1
{
public static void main(String s[]) throws CloneNotSupportedException
{
Tree tree = new Tree();
Tree tree1 = (Tree)tree.clone(); //////////!!! this line is
//giving error
}
}
class Tree implements Cloneable
{
protected Object clone() throws cloneNotSupportedException
{
super.clone();
return this;
}
String name = "name";
}
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
Hi bhartesh,
thanks for the reply. yes now the code is working fine. but my doubt is not clear. because when I modified my code like this it's working fine



while my earlier code that goes as below

was not working.

can somebody pls. run both of these and explaimn why one is working while other is not. because I've not overridden clone() method in any of them.

all responses are highly appreciated.
regards
deekasha


Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi deekasha,
Believe what you are seeing is due to the protected access of the clone() method.
According to the JLS §6.6.1 protected members are accessible to code within the same package and subclasses in other packages if the outside code is involved in the implementation of the of the class.
In your first example, the class CloneTest1 is NOT involved in the implementation of an Object. You are creating a Tree object which implicitly creates an Object where the protected clone() method exists ie Tree is involved in the implementation.
In Baratesh's example, Tree is overriding the Object clone() method. It exists in the same package as CloneTest1, therefore, it is accessible to CloneTest1.
In your second example, the clone() method is being called from within the Tree constructor which is involved in implementing an Object therefore it can access the protected method clone() in Object.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clone method giving error
 
Similar Threads
java SWT application giving error while executing jar file
clone ( )
Doubt about sub-super class relationship
Can anybody explain the error msg when run?
why am I getting verify error ???