aspose file tools*
The moose likes Beginning Java and the fly likes Object Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Object Class" Watch "Object Class" New topic
Author

Object Class

Romesh Binwani
Greenhorn

Joined: May 27, 2008
Posts: 4
Hi All,

Why finalize method in Object class is protected?
Can any one please give me the reason?
cortin flaneir
Greenhorn

Joined: May 26, 2008
Posts: 13
finalize method is call from jvm automatically. when object goes on garbage colect.this method is protected because you can not call it form object of Object class.Object class is base classes of all class then this method is automatically overridden.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Welcome to JavaRanch, Romesh Binwani.

Cortin Flaneir is correct that finalize() is called by the JVM at garbage collection time, but not quite that the method is automatically overridden. In fact the method is rarely overridden; you very rarely need to put anything in the finalize() method in your classes.
cortin flaneir
Greenhorn

Joined: May 26, 2008
Posts: 13
there is some structure degine for framework. for this some method like clone and finalize has been protected. for example. if you use clone method .it is protected. but you have to override this method and call super.clone.then i mean to say that a structure is fallow by the sun java devloper team. they dont want that these methods are pubilc.coz they works in havey enviroment
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
I am afraid I didn't understand our last post, Cortin Flaneir. But I know the clone() method is another which is not frequently used. It is used to obtain a "copy" of the present object, and is dependent on the cloneable interface.
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
I ain't trying to answer. I am just trying to convey what Cortin has tried to explain.I suppose cortin is not very good at english.

He is saying,
The finalize and clone methods have kept protected as per SUN's design framework.The SUN don't want to make those methods public as per their framework.


Still the question is left unanswered that why did those methods have kept protected?
cortin flaneir
Greenhorn

Joined: May 26, 2008
Posts: 13
Finalize is protected and, therefore, is accessible only through this class or a derived class and object class is base class of every java class.this is the only fact. i think.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Yes, I think you are correct that finalize cannot be called from outside its own class. Not sure about clone; find a copy of Effective Java by Joshua Bloch. I think you override clone with public access.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Vishal Pandya:

Still the question is left unanswered that why did those methods have kept protected?


Well, what accessibility would *you* have given to it, and why?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

You don't want all classes to be able to call it, so public is out of the question.
You want subclasses to be able to override it, so private is out of the question.
You want possible subclasses from different packages to be able to override it, so default is out of the question.
That leaves only protected


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object Class