GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes why Finalize() method is protected..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why Finalize() method is protected..?" Watch "why Finalize() method is protected..?" New topic
Author

why Finalize() method is protected..?

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear all

I want to know why finalize method in Object class is protected .and whats the comparision between public and protected in this issue.Because even though if they made protected any class can use this method because every class is a sub class of object.





Creativity is nothing but Breaking Rules
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39070
    
  23
If you are in beginners' then I should tell you to forget all about finalize(). Don't use it.

If you are more advanced, then finalize() is supposed to be called by the JVM, not from any of your classes at all, so it ought not to be public. If it had private or default access, then it would be impossible to override, so it must have protected access.
santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
not from any of your classes at all, so it thought not to be public

hello sir... Even though it is protected all class can overide the method because every class is a sub class of Object class .Then what sort of difference will exists between Protected and public in this issue. protected in the sence only sub class can view public in the sence all classes can view .here even though my class is not extending the object class compiler by default extend the object class.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

santhosh.R gowda wrote:Then what sort of difference will exists between Protected and public in this issue.


always it is good practice to reducing the visibility as much as possible
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Actually, the behavior of "protected" is more subtle than you realize. Imagine three classes P, C1, and C2 (parent, child 1, and child 2.) P defines a protected method m(). Code in C1 can, of course, call m() on instances of P and on instances of C1 itself. But code in C1 can not call m() on instances of C2!

In terms of Object and finalize(), this means that code in any class C can call finalize() on its own instances, and super.finalize(), but that's it -- it can't call finalize on instances of any other class.

This is perfectly in line with the intended purpose of finalize(). Only the system is supposed to call it, but you need to be able to call super.finalize() if you override it. You should never try to call it on any other class.


[Jess in Action][AskingGoodQuestions]
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

Ernest Friedman-Hill wrote: P defines a protected method m(). Code in C1 can, of course, call m() on instances of P and on instances of C1 itself. But code in C1 can not call m() on instances of C2!


I think code in c1 can call m() on instances of c2 in the same Package.

But

If Packages are different
Than
Code in C1 can, cannot, call m() on instances of P . and also code in C1 cannot call m() on instances of C2!

Please clear my speculation on that


Understanding is Everything - Peter Lord
Pratik D mehta
Ranch Hand

Joined: Jul 29, 2010
Posts: 121

I realized now it was a post of 2009 . But now with new jdk update things are different .
Please clear my speculation according to the new update .
 
GeeCON Prague 2014
 
subject: why Finalize() method is protected..?