• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's true.

Check here.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crack or no crack, implementing clone in this case is not an advanced question:
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic