Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to override clone method in Java

Tags:

java

clone

I am confused regarding overriding clone method in the class for which I want cloned object.

Object class has protected object method and as per the protected behavior which is When a method is protected, it can only be accessed by the class itself, subclasses of the class, or classes in the same package as the class.

As every class in Java extends from Object, so it should have clone method but still we are forced to override clone. Why is it required?

Also, I have read at some places to override the clone object and make it public. I wonder, why is it so?

All answers are welcome.

like image 818
Anand Avatar asked Oct 26 '12 18:10

Anand


People also ask

What is necessity of overriding clone method?

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.

Is it necessary to override clone method in Java?

Java Object Cloning lang. Cloneable marker interface. Otherwise, it will throw CloneNotSupportedException at runtime. Also Object clone is a protected method, so you will have to override it.

Why clone () method is protected?

The clone() is protected because it is not declared in the Cloneable interface. Because of this reason, the clone method becomes somewhat useless as it might make the copies of your existing data.

Why is clone () method used in Java?

The clone() method is used to create a copy of an object of a class which implements Cloneable interface. By default, it does field-by-field copy as the Object class doesn't have any idea about the members of the particular class whose objects call this method.


1 Answers

As every class in Java extends from Object, so it should have clone method but still we are forced to override clone

No you are not forced to override the clone method. In inheritance, when you inherit a class, you are not forced to override it's method. Its modifier being public or protected doesn't make much of a difference. However, if you want to invoke a method directly on super class reference, then that method has to be public. Protected methods are accessible only through inheritance. That is you can only access them through subclass reference. Or if you override the method, you can access them through super keyword.

Having said that, you should not override clone method, as it is broken. 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.

public class A {
   private int data;
   public A() {
   }

   public A(A a) {
      this.data = a.data;
   }
}

For more details, I would suggest to go through this chapter of Joshua Bloch's Effective Java, which covers all aspects of using clone method.

Effective Java- Item # 11 - Override clone judiciously

like image 195
Rohit Jain Avatar answered Oct 05 '22 08:10

Rohit Jain