File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes I am getting clone() has protected access in java.lang.Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "I am getting clone() has protected access in java.lang.Object" Watch "I am getting clone() has protected access in java.lang.Object" New topic
Author

I am getting clone() has protected access in java.lang.Object

nitin kumar
Ranch Hand

Joined: May 14, 2002
Posts: 105
Test1.java
-------------
package paul1;

public class Test1 {
public String name;
public Test2 objTest2;

public Test1(){
objTest2 = new Test2();
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setObjTest2(Test2 objTest2){
this.objTest2 = objTest2;
}
public Object getObjTest2(){
return objTest2;
}


}

Test2.java
---------
package paul1;

public class Test2 implements Cloneable
{
public String pass;
public Test2(){
pass = "paul";
}

public void setPass(String pass){
this.pass=pass;
}

public String getPass(){
return pass;
}

public static void main(String[] args)throws CloneNotSupportedException{
Test1 t1 = new Test1();
Object obj = t1.getObjTest2();
Test2 t2 = (Test2)obj;
System.out.println(t2.getPass());
Object obj2 = t1.clone();
Test2 t3 = (Test2)obj2.getObjTest2();
System.out.println(t3.getPass());


}
}

Hi Fiends! I am trying to create an object for Test2 class in Test1 class through a method. It is working fine to call a method in Test2 class.

Now I have another requirement in this code is, to clone the Test1 object to get the details of Test1 as well as Test2 information.

Your help is required, in this aspect.

Thanks
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
It's true.

Check here.


My blood is tested +ve for Java.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
You have to implement a public clone method (being less restrictive is fine) but if you have an option stay away from the brain-dead clone implementation and just add your own public copy method. I have no direct knowledge of this but it would not surprise me to discover that the person who added the clone interface in Java was a programming intern high on crack.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Crack or no crack, implementing clone in this case is not an advanced question:


There is no emoticon for what I am feeling!
Rob Mitchell
Greenhorn

Joined: Dec 10, 2003
Posts: 7


[ December 28, 2005: Message edited by: Rob Mitchell ]
[ December 28, 2005: Message edited by: Rob Mitchell ]

_ ____________________________ _ <br />Rob Mitchell<br />Base Class Technologies, Inc.<br />Java, WebSphere, Domino, Oracle, Web development<br />Rob.Mitchell@comcast.net
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
???

After cloning, this.objTest2 == copy.objTest2, so what's the difference between our code? Isn't it all equally good?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am getting clone() has protected access in java.lang.Object