Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

clone() method problems

 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why is that when we override the clone method with private access modifier it gives a compiler error but when we change it to protected it does not

protected Object clone(){return Object();}\\works fine....but
private Object clone(){return Object();}\\does not...

please explain me this ...
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Varun Nayudu:
why is that when we override the clone method with private access modifier it gives a compiler error but when we change it to protected it does not

protected Object clone(){return Object();}\\works fine....but
private Object clone(){return Object();}\\does not...

please explain me this ...



The basic rule of overriding is you cannot make the access modifier more restrictive bbut you can make it less restrictive.

Since the modifier used in clone methods is protected , you can either keep it protected or make it public , as it is less restrictive - but you cant make it private.
 
author
Posts: 9020
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those of you playing at home - the clone() method isn't on the exam. It might be that ANY method in the API could be used as an example of how OO concepts work generally, so in that case you might see a question that uses the clone() method as an example of a method in a class - but you WON'T need to understand how the clone() method itself works.

hth,

Bert
 
Sheriff
Posts: 9679
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bert...
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic