• 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

Shallow Copy implementation Question

 
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can, but you need to use super.clone().

For example (if you're happy with the default behaviour):
 
Adam Zedan
Ranch Hand
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.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
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 124
C++ Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic