• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Clone Method

 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohhh...got it completely.....

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

thanksss.....
 
reply
    Bookmark Topic Watch Topic
  • New Topic