Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clone Method

 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read about Cloneable Interface? This will help you clear up your doubts.

Please UseCodeTags to post your code.
 
ankur trapasiya
Ranch Hand
Posts: 160
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic