why finalize() does not automatically call its parent finalize() like constructor in java?

StackOverflow https://stackoverflow.com/questions/14474295

  •  17-01-2022
  •  | 
  •  

Question

I have override the finalize() method to do some work while re-claiming the memory space of the objects.But somebody says that i have to call the parent's finalize() in the overriding the finalize() method.But constructor automatically calls it super class constructor unlike finalize() method.

Can you enlighten me something on this...?

Was it helpful?

Solution

It is defined that way in the Java Language Specification #12.6 (emphasis mine):

The finalize method declared in class Object takes no action. The fact that class Object declares a finalize method means that the finalize method for any class can always invoke the finalize method for its superclass. This should always be done, unless it is the programmer's intent to nullify the actions of the finalizer in the superclass. (Unlike constructors, finalizers do not automatically invoke the finalizer for the superclass; such an invocation must be coded explicitly.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top