I read everywhere that if I call clone() without implementing Cloneable interface I will get CloneNotSupportedException. If I implement clone method in a class which does not implement Cloneable, I can still call clone() w/o exception. I mean implementing Cloneable makes no difference. Please elaborate........
-
You might find [this read](http://www.javapractices.com/topic/TopicAction.do?Id=71) interesting. – m0skit0 May 04 '13 at 18:22
-
1Questions is based on a false premiss. – user207421 May 04 '13 at 18:31
-
possible duplicate of [object cloning with out implementing cloneable interface](http://stackoverflow.com/questions/8192223/object-cloning-with-out-implementing-cloneable-interface) – jlordo May 04 '13 at 22:10
3 Answers
Implementing the Cloneable interface just tells the Programmer that this object should have a valid clone method.
If you looked at the Cloneable interface you would find a comment that looks like this
Note that this interface does not contain the clone method. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Even if the clone method is invoked reflectively, there is no guarantee that it will succeed.
The cloneable interface is just a programming practice a programmer should follow if they add an implementation to clone.

- 30,689
- 5
- 75
- 96
I read everywhere that if I call clone() without implementing Cloneable interface I will get CloneNotSupportedException.
Correct, if we're talking about Object.clone().
If I implement clone method in a class which does not implement Cloneable, I can still call clone() w/o exception.
No you can't, if we're talking about Object.clone().
I mean implementing Cloneable makes no difference.
Yes it does. Possibly your class inherits from a class that already implements Cloneable. Without seeing any code it is impossible to know how you arrived this misconception.
Please elaborate.
There's nothing to elaborate on. You're mistaken. That's it.

- 305,947
- 44
- 307
- 483
-
This is an example of a class not implementing Cloneable that calls it without throwing an exception. `public class Cloneer{ public Cloneer clone() { return new Cloneer(); } public static void main(String[] args){ (new Cloneer()).clone(); } }` – FDinoff May 04 '13 at 18:51
-
This is an example of a class that doesn't call Object.clone(), so none of the side-effects specified for Object.clone() apply to it, including NotCloneableException. I suggest you read the Javadoc. Not a real question. – user207421 May 04 '13 at 19:11
-
-
I'm not obliged to account for your confusion. Just read the Javadoc as suggested. Object.clone() throws NotCloneableException if the class doesn't implement Cloneable. Period. Your example doesn't exercise that behaviour, and is not therefore a counter-example. – user207421 May 04 '13 at 19:21
-
@FDinoff: "I read everywhere that ..." Your confusion is because you either read the wrong things or misunderstood what you read. – newacct May 04 '13 at 22:32
Your class has inherited the protected method clone() (and it is able to create a shallow copy of your object) from Object. However, in order to gain access to that method outside the definition of your class, you need to mark your class as Cloneable, thus the implementation.
In the example you gave, if you have created a method with the same signature as clone(), implementing Cloneable simply won't matter to the compiler, because he'll gain access to the method you've requested, so the implementation of Cloneable will be simply empirical. However, I suggest you stick to the good practice approach.

- 1,463
- 2
- 22
- 41
-
"in order to gain access to that method outside the definition of your class, you need to mark your class as Cloneable" No. Access to your method is only dependent on the access specifiers that you apply to your method. Implementing `Cloneable` has absolutely nothing to do with it. – newacct May 04 '13 at 22:28
-
What you say is absolutely right if you have defined clone() in your class. However, if you do not implement Cloneable, nor do you override the method, you can't access it outside because it is, implicitly, protected. Take for instance: public class A { }. You can call clone from inside it, but not when you have an instance (e.g. A a = new A(); a.clone() -> this won't work; but public class A { public void test() {this.clone()}} will). Perhaps I failed to explain what I actually meant. Please excuse my rusty English. Also, if I didn't understand what you meant, I'm open to further explanations – Cristina_eGold May 04 '13 at 22:32
-
You can implement a public `clone()` method and not implement `Cloneable`. The method will be accessible because you implemented it. Conversely, you can not implement a public `clone()` method and implement `Cloneable`. The method will not be accessible outside your class because you didn't implement it. Implementing `Cloneable` is irrelevant. – newacct May 04 '13 at 22:34
-
Please refer to the second paragraph in my response. I think that's what I said as well. At least, that's what I wanted to say... – Cristina_eGold May 04 '13 at 22:38
-
Implementing `Cloneable` or not has no effect on access to any method in ANY circumstance. Not just in his example, in all cases. – newacct May 04 '13 at 22:44
-
Now I understand why we seemed like we didn't speak the same language. By implementing an interface I don't understand only the statement "implement SomeInterface" (simply stating that won't compile anyway, if SomeInterface contained a method), but also implementing the methods DEFINED in that interface. And that makes me not wrong. You're not wrong, either, of course, just referring to a different thing. :) – Cristina_eGold May 04 '13 at 22:53
-