Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Please explain it.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Posts: 21087
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Arun Singh Raaj
Greenhorn
Posts: 27
1
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Dave Tolls
Rancher
Posts: 2911
35
  • Mark post as helpful
  • send pies
  • 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: 55678
161
  • Mark post as helpful
  • send pies
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!