Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason behind Object.clone() is protected [duplicate]

Tags:

java

clone

Possible Duplicate:
Why is the clone() method protected in java.lang.Object?

Here is my test code for checking the clone method working,

class Test{
  int a;
  public void setA(int value){
a = value;
  }
  public int getA(){
   return a;
  }
}
class TestClass{   
   public static void main(String args[]){
   Test obj1 = new Test();
   obj1.setA(100);
   Test obj2 = obj1.clone();
   System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
   obj2.setA(9999);
   System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
 }
}

Throws compilation error: clone() has protected access in java.lang.Object at obj1.clone()

  1. What I am doing wrong here?
  2. What is the reason behind clone() is protected?

Thanks

Edit along with Answer: Well at last I see my test harness is working when I implemented the Cloneable interface and overriden the clone method. It's not working with just overriding the clone() method from Object class. Here is the modified code,

class Test implements Cloneable{
 int a;
 public void setA(int value){
a = value;
 }
 public int getA(){
return a;
 }
@Override
protected Test clone() throws CloneNotSupportedException{    
  return(Test) super.clone();  
  }   
}
class TestClass{   
  public static void main(String args[]){
     Test obj1 = new Test();
   obj1.setA(100);
   try{
     Test obj2 = (Test)obj1.clone();
     System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());
     obj2.setA(9999);
     System.out.println("obj1 A:"+obj1.getA()+" obj2 A:"+obj2.getA());       
   }catch(Exception e){
     System.out.println("ERror"+e);
     }         
    }
  }

2. Reason for clone() method being protect: I found this from the book Core Java,

The clone method is a protected method of Object, which means that your code cannot simply call it. Only the Employee class can clone Employee objects.

There is a reason for this restriction. Think about the way in which the Object class can implement clone. It knows nothing about the object at all, so it can make only a field-byfield copy. If all data fields in the object are numbers or other basic types, copying the fields is just fine.

But if the object contains references to subobjects, then copying the field gives you another reference to the subobject, so the original and the cloned objects still share some information.

Hope this is helpful to others

like image 542
droidsites Avatar asked Oct 25 '11 10:10

droidsites


People also ask

Why object 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.

What is clone () in Java?

The Java Object clone() method creates a shallow copy of the object. Here, the shallow copy means it creates a new object and copies all the fields and methods associated with the object. The syntax of the clone() method is: object.clone()

What is the purpose of the following code protected object clone () throws?

Thrown to indicate that the clone method in class Object has been called to clone an object, but that the object's class does not implement the Cloneable interface.

Why clone in object class and protected and we are implement cloneable which is a marker?

Cloneable is a marker interface. The clone() method isn't defined by the Cloneable interface. The clone method in the Object class is protected to prevent a client class from calling it - Only subclasses can call or override clone, and doing so is a bad idea.


1 Answers

You should override the clone method in the Test class.

Why it is protected is discussed here although there doesn't seem to be a consensus.

like image 70
Jim Avatar answered Oct 24 '22 03:10

Jim