Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the clone method clone overridden methods?

If I clone an instance of the following class, and overridde a method when instancing, will the clone have the overridden method? I haven't found anything regarding this behavior in https://docs.oracle.com/javase/7/docs/api/java/lang/Cloneable.html nor https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone() .

public class ToBeCloned implements Cloneable{
    public int returnInt() {
        return 1;
    }
    public void printTest() {
        System.out.println("returnInt():"+returnInt()+"\nToBeCloned Original");
    }
    @Override
    public ToBeCloned clone() throws CloneNotSupportedException {
        return (ToBeCloned) super.clone();
    }
}
like image 862
HopefullyHelpful Avatar asked Aug 24 '20 18:08

HopefullyHelpful


People also ask

Can we override clone method?

From Java 1.5 onwards an overriding method can return subclass of return type declared in original method, which means you can return sub class from clone method. It is known as co-variant method overriding.

Why do we override clone method?

Because, for a class to be cloned, you need to implement the Cloneable interface. And then your class uses the clone method of Object class instead. Because, Cloneable interface doesn't exactly have any method for cloning . It would be a better option to use Copy Constructor instead.

Is it necessary to override clone method in java?

Therefore, while overriding the clone() method it is recommended to declare it public instead of protected so that it is access-able from a class in any location in the system. While overriding methods the method in the child class must not have higher access restrictions than the one in the superclass.

What is clone () method?

The clone() method of Object class is used to clone an object. The java. lang. Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException.


2 Answers

If you do something like

new ToBeCloned() { @Override...}

it is just a short way of creating a subclass and instantiating it. If you clone that instance, you get another instance of the same anonymous subclass, with all the same methods.

like image 117
khelwood Avatar answered Oct 23 '22 09:10

khelwood


The answer is yes, the clone will contain the overridden methods atleast in javaSE-1.8.

This is illustrated by the following programm and it's output:

public class OverridingMethods {
    public static void main(final String[] args) {
        final ToBeCloned toBeCloned1 = new ToBeCloned();
        final ToBeCloned toBeCloned2 = new ToBeCloned() {
            @Override
            public int returnInt() {
                return 2;
            }
            @Override
            public void printTest() {
                System.out.println("returnInt():"+returnInt()+"\nToBeCloned Overridden");
            }
        };
        ToBeCloned toBeCloned3 = null;
        ToBeCloned toBeCloned4 = null;
        ToBeCloned toBeCloned5 = null;
        try {
            toBeCloned3 = toBeCloned1.clone();
            toBeCloned4 = toBeCloned2.clone();
            toBeCloned5 = toBeCloned4.clone();
        } catch (final CloneNotSupportedException e) {
            e.printStackTrace();
        }
        toBeCloned1.printTest();
        toBeCloned2.printTest();
        toBeCloned3.printTest();
        toBeCloned4.printTest();
        toBeCloned5.printTest();
    }
}

The output of the programm is the following:

returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():1
ToBeCloned Original
returnInt():2
ToBeCloned Overridden
returnInt():2
ToBeCloned Overridden

This proofs that the overridden method is kept, even if cloning already cloned instances.

like image 3
HopefullyHelpful Avatar answered Oct 23 '22 09:10

HopefullyHelpful