Why is finalize() method protected?
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Why is this method designated as a protected one.
Tx in advance
Regards
Tx in advance
Regards

posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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".
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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: 982
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thnak you....for the explanation!!!
but could nt they ahve made it as a public method..
but could nt they ahve made it as a public method..
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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 coderanch, well, we already talked about it.
Then the best option is to make it protected.
Does that sound logical to you?
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 coderanch, 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: 982
posted 19 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Edwin!!!
Thats is very precise ...one...
Thats is very precise ...one...

| I guess I've been abducted by space aliens. So unprofessional. They tried to probe me with this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









