Java 5 introduced generics, and they were added to many interfaces in the java.lang
package. However, Cloneable
did not get generics. I wonder why?
Edit: In reply to the answers of @Jon and @litb, and the comment of @Earwicker, I was thinking Cloneable
might be:
public interface Cloneable<T> {
public T clone();
}
Here T clone();
overrides Object.clone()
, giving it a covariant type. I believe this would still be backwards compatible and increase type safety. So why not?
Edit 2: As can be seen in the answers (and comments) below, the interface suggested above would break backwards-compatibility. Since Object.clone()
is protected
, rewriting it in the interface would force all implementers to provide a public
implementation, which class designers might not want to (i.e. they might opt to keep it protected
).
The Cloneable interface itself is empty; it is just a marker interface used by Java to ensure that using the clone method is legal. Doing it this way also removes the ability to make use of generics to ensure type safety: class Foo implements Cloneable { // Valid.
Object's clone() method probably just checks it using: this instanceof Cloneable. The reason that the clone() method is defined in the Object class, is because some 'magic' is needed to actually make a clone. First of all, a new object has to be created without the use of a constructor.
Cloneable interface is a marker interface. It was introduced in JDK 1.0. There is a method clone() in the Object class. Cloneable interface is implemented by a class to make Object.
Cloneable is a marker interface. The clone() method isn't defined by the Cloneable interface. The clone method in the Object class is protected to prevent a client class from calling it - Only subclasses can call or override clone, and doing so is a bad idea.
The Cloneable interface doesn't contain any members. What would be the point of making it generic?
(If Cloneable contained the clone() method, it would make sense - but that's declared in java.lang.Object.)
EDIT: clone() is in java.lang.Object as it has an implementation (which does a field-by-field copy). A better design would be to have something like .NET's MemberwiseClone() as a protected method in Object, and then a public clone() method within the interface itself. I don't know why that design wasn't chosen.
(In .NET, ICloneable isn't generic because it existed before generics - the different nature of .NET generics prevents a previously non-generic type from becoming generic.)
In both Java and .NET, however, the "normal" cloning API is generally regarded as a bad thing, as it says nothing about what depth of cloning should be performed.
If Cloneable
was Cloneable<T>
, then it would be impossible to do this:
class A extends B implements Cloneable<A>
class B implements Cloneable<B>
You can't implement the same interface with different parameters, and class A
implements Cloneable<A>
and Cloneable<B>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With