aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clone Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Clone Method" Watch "Clone Method" New topic
Author

Clone Method

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

class A
{
public void doKooll() throws CloneNotSupportedException
{
A b=(A)this.clone(); //works fine
}

}
public class CloneTest
{
public static void main(String[] args)
{
A a= new A();
try
{
a.doKooll();
A b=(A)a.clone(); // ** compile time error here stating clone has protected access in Object.
}
catch(CloneNotSupportedException e)
{
System.out.println("err");
}
// A b=(A)a.clone();
}
}

would anyone please tell that if clone is protected in object then it must be accessible from the same package but here it is not allowing to do so ..... why? btw output of program is "err"


OCPJP(83%)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

Did you read about Cloneable Interface? This will help you clear up your doubts.

Please UseCodeTags to post your code.

Mohamed Sanaulla | My Blog
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

yeah i have read in order to an object to be cloneable it must implement marker interface Cloneable but my question is if the method in object class is protected then it should be accessible from the same package containing the class code.....
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

ankur trapasiya wrote:yeah i have read in order to an object to be cloneable it must implement marker interface Cloneable but my question is if the method in object class is protected then it should be accessible from the same package containing the class code.....


clone() is part of Object and which is in java.lang package. You cannot access the method via the instance from a different package. Either you can access it via inheritance(The first type of use in your code) or access it in the same package.
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

ohhh...got it completely.....

i was accessing it in different package than java.lang.....

thanksss.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clone Method
 
Similar Threads
Clone method giving error
I have some vague concept about Cloneable....
Object's method
Cloning in Java
I am getting clone() has protected access in java.lang.Object