aspose file tools*
The moose likes Java in General and the fly likes what is the use of Clonable interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is the use of Clonable interface?" Watch "what is the use of Clonable interface?" New topic
Author

what is the use of Clonable interface?

sujitha reddy
Greenhorn

Joined: Oct 20, 2006
Posts: 15
what is the use of Clonable interface? Clone method is in Object class,while we are using Clone method we must implement clonable interface, why?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Please reserve this forum for advanced questions. I have moved this to the intermediate forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nav Letha
Greenhorn

Joined: Oct 22, 2006
Posts: 8
Or why not?
Cloneable is an interface and you need to implement it if you want to clone the object.Object's clone method only gives you basic implementation.

you can verify if an object implements Cloneable by using 'instanceof'.

If you dont implement Cloneable, it will throw CloneNotSupportedException.

side note:Note that object's clone does shallow copy meaning if you have a complex member, it will copy only referrence and it is your responsibility (if situation demands it) to do deep copy.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by sujitha reddy:
what is the use of Clonable interface? Clone method is in Object class,while we are using Clone method we must implement clonable interface, why?


It's called a marker interface and is widely considered an anti-pattern. Why the original designers decided to use a marker interface I'm not sure. In fact there's an RFE in Sun's bug database with a suggested mechanism to replace Cloneable:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5101827
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Nav Letha:
Or why not?


How about the fact that it's an interface that doesn't actually declare the method it's defining the behavior for? Object declaring a method that may or may not actually be implemented is another absurdity arising from it. Unfortunately this sad fact also results in an exception that must be declared since a subclass may or may not actually be Cloneable and if that's not bad enough it's a checked exception. While not caused by the use of a marker interface, it's also problematic that it relies on what are essentially undocumented conventions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the use of Clonable interface?