Why I must override clone if i want cloneable class? All classes extends from Object
, so why I must override the Object clone method? Why I cant just invoke the original Object clone method?
Asked
Active
Viewed 469 times
7

Shelef
- 3,707
- 6
- 23
- 28
-
You might want to checkout this answer as well: http://stackoverflow.com/questions/3652748/cloning-objects-in-java-3-questions/3652767#3652767 – nkr Mar 18 '13 at 12:55
-
You can always write a copy method in your class. You don't have to override anything. – Gilbert Le Blanc Mar 18 '13 at 13:03
2 Answers
8
It's one of the many "design flaws" in the JDK.
Clonable
should have been an interface with a clone()
method, but instead it's a marker interface and Object
has a "do nothing" implementation of the clone()
method... and you're left with your question.
If you're interested, this answer lists some other "mistakes" in java.
1
See here: http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Cloneable.html
Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
Also I guess this discussion would be helpful for you: Confusion about cloneable interface and object.clone() in java