Shallow Copy implementation Question

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

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

Posts: 4568

9

posted 6 years ago

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.

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

Posts: 4568

9

posted 6 years ago

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.

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.

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'.

'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'.

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..