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
).