Interview Questions

Why wasn't the .clone() method specified in the java.lang.Cloneable interface ?

Java Interview Questions for Cloning and Cloneable


(Continued from previous question...)

Why wasn't the .clone() method specified in the java.lang.Cloneable interface ?

Cloneable doesn't contain the clone method. That means you can't test if something is an instance of Cloneable, cast it to Cloneable, and invoke clone.

Created an interface called PublicCloneable, it contains the clone method and specifies that it is public.

public interface PublicCloneable extends Cloneable {
public Object clone();

In Java, there is this weird concept of marker interfaces. The Cloneable interface has no methods or fields and serves only to identify the semantics of being cloneable.

Often you will come across interfaces in Java that have no behavior. In other words, they are just empty interface definitions. These are known as marker interfaces. Some examples of marker interfaces in the Java API include:
* java.lang.Cloneable
* java.io.Serializable
* java.util.EventListener

Because the clone method is implemented in the Object class due to its "special" condition: the memory copy of objects of any kind.

(Continued on next question...)

Other Interview Questions