Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Java's Cloneable Interface Not Generic?

Tags:

java

generics

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

like image 587
Hosam Aly Avatar asked Dec 23 '08 08:12

Hosam Aly


People also ask

Why clone method is not in cloneable interface?

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.

Why should we implement cloneable interface if we have to clone an object though clone method comes from object class?

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.

What type of interface is cloneable?

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.

Why clone in object class and protected and we are implement cloneable which is a marker?

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.


2 Answers

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.

like image 196
Jon Skeet Avatar answered Sep 18 '22 22:09

Jon Skeet


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

like image 28
Martin Zinkevich Avatar answered Sep 21 '22 22:09

Martin Zinkevich