Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

clone() method problems

 
Varun Nayudu
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • 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 ...
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • 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.
 
Bert Bates
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • 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
 
Ankit Garg
Sheriff
Posts: 9512
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bert...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic