Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is #clone() not in the Cloneable interface?

Tags:

java

I was reading up on performing a deep-copy of an array correctly, however I was confused about how the #clone() is implemented. It is a member of the java.lang.Object class, and yet if you read the javadocs:

First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown.

So why define the clone method there in the first place? Surely if a method can only be used when an interface is present, you'd put the method in the 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.
    @Override
    public Object clone() throws CloneNotSupportedException {
        // ...
    }
}

class TypeSafeFoo implements Cloneable<TypeSafeFoo> { // Not valid.
    @Override
    public TypeSafeFoo clone() throws CloneNotSupportedException {
        // ...
    }
}

Why has Java done it this way? I'm sure they have legitimate reasons, but I can't seem to figure it out.

like image 225
Phil K Avatar asked Dec 02 '12 14:12

Phil K


People also ask

Why is the sky blue short answer?

The Short Answer: Sunlight reaches Earth's atmosphere and is scattered in all directions by all the gases and particles in the air. Blue light is scattered more than the other colors because it travels as shorter, smaller waves. This is why we see a blue sky most of the time.

How is the sky blue?

The scattering caused by these tiny air molecules (known as Rayleigh scattering) increases as the wavelength of light decreases. Violet and blue light have the shortest wavelengths and red light has the longest. Therefore, blue light is scattered more than red light and the sky appears blue during the day.

Why the sky is blue Class 10?

When a white light (from sun) enters the earth's atmosphere, it gets scattered away due to the atmospheric particles. Since, blue colour has the minimum wavelength, so blue colour scatters the most and thus the sky appears blue.

Why is the sky blue and white?

Closer to the horizon, the sky fades to a lighter blue or white. The sunlight reaching us from the horizon has passed through even more air than the sunlight reaching us from overhead. The molecules of gas have rescattered the blue light in so many directions so many times that less blue light reaches us.


2 Answers

The cloning contract in Java dictates that each clone implementation must first obtain the cloned instance from super.clone(). This creates a chain that always ends with the call to Object.clone, and that method contains "magical" native-level code that makes a binary copy of the underlying raw struct which represents the Java object. If this mechanism didn't exist, clone would fail to be polymorphic: the Object.clone method produces an instance of whatever class it is called on; this cannot be reproduced without native code.

This is why the Object.clone method could not have been avoided. Cloneable could have contained a clone method, but it would create issues regarding the throws clause. The way it stands you are free to declare clone with no declared exceptions, or to declare arbitrary exceptions. This flexibility would not be possible if the method was already declared in the interface.

Bear in mind that Generics would be of little use for cloning: imagine protected T clone() in Object: where would T come from? Would we need Object<T> and force each and every class in Java universe to be parameterized on itself, and all this just to make this semi-deprecated mechanism work a tiny bit better? Keep also in mind that this code is perfectly legal:

public class TheMightyOne implements Cloneable {
   @Override public TheMightyOne clone() {
     return (TheMightyOne) super.clone();
   }
}

You can call it:

TheMightyOne one = new TheMightyOne();
TheMightyOne two = one.clone(); // do downcasts needed
like image 172
Marko Topolnik Avatar answered Sep 24 '22 19:09

Marko Topolnik


To deal with creating the clone and basic field copying, clone needs to inherit a method implementation. A Cloneable class can appear anywhere in the hierarchy, and may need to extend a specific superclass to do its primary job. The only superclass from which all possible Cloneable classes can inherit implementation is Object.

Cloning was defined long before generics.

like image 33
Patricia Shanahan Avatar answered Sep 22 '22 19:09

Patricia Shanahan