• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

protected behaviour of clone method is surprising !

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying with cloning function capabilities.
I have found strange thing with it.
Though my class implements cloneable interface, its object
cannot be cloned in some other class, though CloneNotSupported
exception catching facility provided in that other class.
I get a compilation error for this, saying class in which object
is getting cloned is not a super class, for object being cloned.
To my surprise,
when I extend the class with the one, in which I am going to clone this
class it works without hassles.
Doubt is, since clone method is protected in Object class, why it cannot be directly used ?
For better clarification I am providing code alongwith.
The code which works,
public class CloneTest
{
public static void main(String args[]) throws CloneNotSupportedException
{
CloneTest ct = new CloneTest();
ct.cloneFunction();
}
public void cloneFunction() throws CloneNotSupportedException
{
CloneableObject coOriginal = new CloneableObject();
CloneableObject coCloned = (CloneableObject)coOriginal.clone();
System.out.println("Original Object Value "+coOriginal.getI()+" & Clone Reference "+coOriginal.toString());
System.out.println("Cloned Object Value "+coCloned.getI()+" & Clone Reference "+coCloned.toString());
}
}
class CloneableObject extends CloneTest implements Cloneable
{
private int i=10;
public int getI()
{
return i;
}
}
I am not getting, why CloneableObject has to extend with CloneTest class ?
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One request: please use the UBB CODE tags when you post code for review. It makes your work much easier to read. For example:

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
[This message has been edited by Michael Ernest (edited December 30, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivaji,
You don't need to extend the CloneTest class. What you need to do is implement the clone interface. That means that you need to have a method like the following:
protected Object clone()
{
return( new CloneableObject() );
}
inside your CloneableObject class. If you don't have that then you are dealing with an abstract class and you can't instantiate an abstract class.
Regards,
Manfred.
 
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic