jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Shallow Copy implementation Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Shallow Copy implementation Question" Watch "Shallow Copy implementation Question" New topic
Author

Shallow Copy implementation Question

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

I a familiar by the concept of shallow and deep copies so i was reading online on how to implement shallow copies and a simple working example which i got is :


Question:
Since I am fairly new to java but as far as i have read about interfaces is that when you create an interface you only specify the signature , and the class
which implements the interface has to provide a body to that signature so i believe the interface Cloneable might look something like this

interface Cloneable
{
Object clone();
.....................
.............................
}

Now I am confused as in the above example why hasn't the class Test provided a body to the clone method and how is it working ???

Another question which i wanted to ask is , why is the .clone protected method not available to the instance of the class such that i could go like a.clone() ?? since simple class is inheriting from the Cloneable interface .. I believe it should have been available to the instance of the class.. could anyone tell me where am i going wrong


Don’t look where you fall, but where you slipped
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Yes, the Cloneable interface is a bit confusing. It could have contained the clone() method, but if you check the documentation you'll see it doesn't. It's actually a marker interface - an interface with no content.

The Javadocs also say:
By convention, classes that implement this interface should override Object.clone (which is protected) with a public method.


If you follow that advice, you'll be able to call clone() directly.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Matthew Brown wrote:Yes, the Cloneable It could have contained the clone() method, but if you check the documentation you'll see it doesn't.
It's actually a marker interface - an interface with no content.


Do you mean it could have contained the clone signature ?? I thought interfaces cant have any methods with a body ?? its upto the class implimenting the interface to provide the body...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Yes, that's what I meant. If clone() had been added to Cloneable, then any class implementing it would have been forced to implement a public method, as opposed to the current situation of implementing it by convention. There's probably a good reason for it, though.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Matthew Brown wrote:Yes, that's what I meant. If clone() had been added to Cloneable, then any class implementing it would have been forced to implement a public method, as opposed to the current situation of implementing it by convention. There's probably a good reason for it, though.


Thanks for pointing me in the right direction ... looks like ill have to read a little bit on Marker interfaces (Serializeable,Cloneable) before i make any further comments on it.. but my other question remains unanswered why is the .clone protected method not available to the instance of the class such that i could go like a.clone() ?? since simple class is inheriting from the Cloneable interface .. I believe it should have been available to the instance of the class.. where am i going wrong and why isnt an instance of simpleclass able to access clone ?
As far as i know about

Protected Fields is :
These fields are available to all derived classes and their instances independent of their package.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

It can, but you need to use super.clone().

For example (if you're happy with the default behaviour):
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Matthew Brown wrote:It can, but you need to use super.clone().



Thanks for the answer again.. So if the instance can access the .clone methods then in my above code the statement


could have been replaced by
//Wrong
unfortunately there is no .clone method available why is that ??


Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

.clone method will not be available

came across this which gives a neat explaination


Question: clone() not accessible from outside on a Cloneable instance - why?


Answer: clone() is defined as a protected method in the Object class. When we implement the marker interface Cloneable then only that class gets the license to call the default Object.clone() method i.e., only the methods of that implementing class can call the inherited clone() method. The Object.clone() method carries a protected access, so even if we want the default clone() (Shallow Copy) to be called from outside, we need to override the clone() method in the class implementing Cleanble and promote the access specifier of the overriden clone() method to 'public'.


class SampleA implements Cloneable{

...

public Object clone(){

super.clone();

}

...

}

public class CloningDemo {

public static void main(String [] args){

...

SampleA sampleA = new SampleA();

sampleACloned = sampleA.clone();

...

}

}

'sampleACloned = sampleA.clone();' - this statement is valid in this case as we have overriden the clone() method with a 'public' access specifier in the class SampleA. Otherwise, it'll throw a compile-time error "method clone() has protected access in class java.lang.Object'.

courtesy :http://geekexplains.blogspot.com/2008/06/clone-not-accessible-from-outside-on.html

Summary: In short if a marker gives you an access to a protected method then that method is only accessible to the methods of the class . Why is that i really dont know but would love to find out
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4342
    
    7

Adam Zedan wrote:unfortunately there is no .clone method available why is that ??

It would be available if you'd overridden it like I've been describing.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Matthew Brown wrote:
Adam Zedan wrote:unfortunately there is no .clone method available why is that ??

It would be available if you'd overridden it like I've been describing.


I know it would be available if we override the clone method. But my question was why wasnt it already available (without overriding) since it was marked as a protected method in the Object Class.
Anyways we know now... Thanks for helping me oiut.. Really appreciate it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Shallow Copy implementation Question
 
Similar Threads
clonning
cloning doubt
pass by reference problem
clone method
Object cast in Prototype pattern