• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why Finalize() method is protected..?

 
santhosh.R gowda
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
Campbell Ritchie
Sheriff
Pie
Posts: 49796
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pratik D mehta
Ranch Hand
Posts: 121
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic