This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes java.lang.object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.object" Watch "java.lang.object" New topic
Author

java.lang.object

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
java.lang.Object class is availble to each and every type of class and iterface,then why some methods of Object class are protected ?

please explain .

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Can you explain in what circumstances you would use protected access?
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
when you want to give access in package and subclasses to out of the package.then protected is used as access specifier.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18711
    
    8

Correct. And therefore those protected methods were declared protected so that those rules would apply to them.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Members should be as private as possible.

clone() is a mechanism that should only be used if a subclass of Object allows it. That's why it's protected. A subclass should determine whether it makes it public.

finalize() would ideally be package private, but subclasses have to be able to provide additional cleanup code, therefore the method is made protected.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.lang.object