File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object Class

 
Romesh Binwani
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Why finalize method in Object class is protected?
Can any one please give me the reason?
 
cortin flaneir
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic