• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clone method giving error

 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic