• 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

how to use clone()

 
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i created these classes and unable to find the problem,while i am trying to compile.
It says that clone() is protected in Object,even i read that Object is superclass for all classes we create.

the error is:

Any One tell me what is wrong with the code??
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your duplicate() method adds nothing functionally to the clone() method. It's pointless.

As the error method it teling you, clone() is protected. Since it's not public, you can't just call it on any old object.


 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
protected method can be invoke only in inheritance tree or inside a package.
So if you move your file to java.lang package, your code will compile fine.
Perhaps it is not good idea:)

Best reagards.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don’t have access to a protected member of Object in that class.
The problem is not about clone() but about protected access. You can read about clone() here. It’s the old edition. You can find the new edition by looking through our book review pages.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piotr Majcher wrote:. . . So if you move your file to java.lang package, your code will compile fine. . . .

We shall remember you on 1st April
 
Nirvikalp Rao
Greenhorn
Posts: 27
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piotr Majcher wrote:Hi,
protected method can be invoke only in inheritance tree or inside a package.
So if you move your file to java.lang package, your code will compile fine.
Perhaps it is not good idea:)

Best reagards.


thanks for the reply,please tell me can't we invoke protected methods out side the package if we inherit them.And
each class inherits "Object"
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the link I gave you.
 
reply
    Bookmark Topic Watch Topic
  • New Topic