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

后端 未结 3 2025
盖世英雄少女心
盖世英雄少女心 2021-01-31 20:13

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

3条回答
  •  孤街浪徒
    2021-01-31 20:36

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

提交回复
热议问题