aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone() method problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone() method problems" Watch "clone() method problems" New topic
Author

clone() method problems

Varun Nayudu
Ranch Hand

Joined: Jun 26, 2006
Posts: 157
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 ...


SCJP 1.5, SCWCD 1.5
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
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.


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Thanks Bert...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
Consider Paul's rocket mass heater.
 
subject: clone() method problems