File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
nitin kumar
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3640
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true.

Check here.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Crack or no crack, implementing clone in this case is not an advanced question:
 
Rob Mitchell
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


[ December 28, 2005: Message edited by: Rob Mitchell ]
[ December 28, 2005: Message edited by: Rob Mitchell ]
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
???

After cloning, this.objTest2 == copy.objTest2, so what's the difference between our code? Isn't it all equally good?
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic