• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

What is a cloneable interface and how many methods does it contain?

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If it is not any method then why we use these?

Please explain it.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See java.lang.Cloneable in the Javadoc. And if there is still confusion, please elaborate the question.

Henry
 
Rd Dari
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry sir,

I am little confuse about it I read that link.

Please give me simple example to understand easily.

Thank you!
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Basically, if a class wants to be clonable, it just needs to implement the Cloneable interface -- which has one method, the clone() method. The clone() method is called when the application needs to make a copy of a cloneable instance.

Henry
 
Rd Dari
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok I have understand now

Thanks a lot for giving me early response.

I can understand that www.coderanch.com why is so popular.

Thanks once again.
 
Sheriff
Posts: 22801
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:the Cloneable interface -- which has one method, the clone() method.


No it doesn't. The clone() method is declared in Object as protected. Cloneable does not have it, because that would mean that the clone() method of any class that implements Cloneable would need to become public. For some reason Sun decided it should be possible to keep it protected.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:No it doesn't. The clone() method is declared in Object as protected. Cloneable does not have it, because that would mean that the clone() method of any class that implements Cloneable would need to become public. For some reason Sun decided it should be possible to keep it protected.



Yeah, very good point. Maybe "has" is too strong a word. The Cloneable interface and clone() method should be used together -- but the interface doesn't specify the method. Sorry for the confusion.

Henry
 
Ranch Hand
Posts: 240
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So @henry, you mean to say that cloneable interface has a clone() method but implementation has been done in Object class?
 
Marshal
Posts: 79701
381
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:So @henry, you mean to say that cloneable interface has a clone() method but implementation has been done in Object class?

No.

There is a rudimentary implementation in the Object class, but you cannot simply clone an Object because its clone() method has protected access and is therefore inaccessible outside the java.lang package.So what you do is to override the clone() method with public access and also implement the Cloneable interface, which, as you can see from this link, is a tagging interface with no methods. You must do both.
If you find a copy of Effective Java by Joshua Bloch you can find more about the clone() method (page 54) and you will find a different opinion about marker/tagging interfaces (page 179). You will see that Bloch warns that the clone() method is difficult to use.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not actually the case.


The above will happily compile since clone() is protected, so accessible by any class (since they all extend Object).
However, it will throw the exception as Test does not implement Cloneable.
 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is different from what I showed; I showed plain simple Object whereas you are inside the Test class and the inherited clone() method is now inside the same package so it is accessible. You still have to handle the Exception which appears to be checked.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Arun Singh Raaj wrote:So @henry, you mean to say that cloneable interface has a clone() method but implementation has been done in Object class?

No.

There is a rudimentary implementation in the Object class, but you cannot simply clone an Object because its clone() method has protected access and is therefore inaccessible outside the java.lang package.So what you do is to override the clone() method with public access and also implement the Cloneable interface, which, as you can see from this link, is a tagging interface with no methods. You must do both.
If you find a copy of Effective Java by Joshua Bloch you can find more about the clone() method (page 54) and you will find a different opinion about marker/tagging interfaces (page 179). You will see that Bloch warns that the clone() method is difficult to use.

 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you got a question?
 
ganesh chowdhary
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Have you got a question?

 
ganesh chowdhary
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Have you got a question?

I have a question for u.Since every class extends Object class,why can't it access the protected clone method
 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use abbreviations like “u”; they are difficult for non‑English‑speakers to understand.

You want to be able to call myObject.clone() from outside the class. That won't work because the protected modifier permits access within the same package or in code responsible for the implementation, which may be in a subclass.Try this:-The only way you can get such a method to execute is to override it with public access.
 
ganesh chowdhary
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't use abbreviations like “u”; they are difficult for non‑English‑speakers to understand.

You want to be able to call myObject.clone() from outside the class. That won't work because the protected modifier permits access within the same package or in code responsible for the implementation, which may be in a subclass.Try this:-The only way you can get such a method to execute is to override it with public access.

thanks a lot.
Cloneable should be implemented and there should be a provision of public access clone method for access in outside the package and outside implemented class.Superb Campbell.
 
Campbell Ritchie
Marshal
Posts: 79701
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't quote the whole of the preceding post; that simply makes the thread longer and longer.

And . . . “That's a pleasure ” Find the Joshua Bloch references I showed you earlier.
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic