• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is finalize() method protected?

 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this method designated as a protected one.

Tx in advance
Regards
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this method is inherited from Object. And in Object, it is defined as protected and if any class want to override it, should be at least put the access modifier "protected".
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finalize method is designed in such a way that finalize method for any class can always invoke the finalize method for its superclass.





In above code, SubClass instance is having anotherresource and the associated superClass instance is also having HeavyResource. Now to free these resources call is made at line 1 by calling shutdown() method to close that resource (not implemented here). Then a call is made to super.finalize() to free the resource consumed by super intance.

If finalize is private or friendly in Object class, then no class outside java.lang package can make a call to finalize().
Even u can't get it thru inheritance so can't override it.

Since every class extends Object class, so least accessibility required is protected either calling super.finalize() or in overridding.

regards
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnak you....for the explanation!!!


but could nt they ahve made it as a public method..
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The finalize method is intended to be executed by the JVM just before GC collects garbage.

What is the minimum accessibility you could give to a method so that it could be overriden by any other sublass of object?

The answer is: protected.

You can override the method and do it public if you want, however, according to the contract of the finalize method, it would not make any sense.

A method is public if you pretend to offer some service through it, but no other class or object should invoke finalize directly. It should only be invoked by JVM before garbage collection.

Then, the best you can do is declare it protected.

Why?, well, If you do it private, it cannot be overriden, if you do it package accessible it cannot be overriden by classes in other packages. If you do it public, well, we already talked about it.

Then the best option is to make it protected.

Does that sound logical to you?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Edwin!!!

Thats is very precise ...one...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic