• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

clone method?

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The clone method is defined as a protected method in java.lang.Object class and every class in Java extends Object. Since subclass can inherite the protected method of its superclass, every class is suppoed to inherite clone method from Object class.

But when I want to invoke clone method of a object, the compiler reports the following error:



Why cant I invoke clone method??


[ March 05, 2005: Message edited by: Jon Lee ]
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Do you have a class CloneTest. Just put the rest of your code in the thread. Holding back on the code makes it harder to answer.
 
Jon Lee
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FileName: CloneTest.java

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rules governing this are subtle, and have been discussed in this forum many times before; use our Search function to search for "clone" in this forum and you'll find full explanations.

But in any case, to make clone() work for a class, that class has to do two things:

1) Override clone to call super.clone(); and

2) Implement the (empty) Cloneable interface.

Therefore, the minimal class on which you can actually call clone() looks like this:



As I said, why this is has been explained here many times before.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It may seem weird that you need to override clone(), but the key is that you must give it public access to be able to call it from a different class. Protected methods are inherited by the subclass, but they aren't visible outside the class or subclass.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For detailed explanation, see this appendix from Bruce Eckel's Thinking in Java...

http://www.faqs.org/docs/think_java/TIJ319.htm
 
Jon Lee
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx guys, now I have the answer.

Cheers
 
It sure was nice of your sister to lend us her car. Let's show our appreciation by sharing this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic