if i want to dereference an object(using delete operator) of DERIVED class which is being referenced using a BASE class reference, which destructor would b called automatically??
BASE class destructor or DERIVED class destructor??

7 Years
Discussion Span
Last Post by JamesCherrill

Are you sure you are talking about Java? Java doesn't have destructors. It has a finalise() method, which is of roughly zero use. If you have finished with an object just ensure there are no remianing references to it. If you have a reference variable that won't go out of scope, just set it to null.


Java has no destructor. If you want to dereference an object of DERIVED class, you may assign a null to the reference of the object so that calling the System.gc() will return the corresponding space to the OS sometime later. The garbage collector soon or later will collect it no matter if you put the line code: System.gc() or not.

# every class inherits the finalize() method from java.lang.Object
# the method is called by the garbage collector when it determines no more references to the object exist
# the Object finalize method performs no actions but it may be overridden by any class

to know more.

Edited by tong1: n/a


Beware of the finalise() method - it's totally unreliable - there are no guarantees as to when it will be called, and it may never be called at all (eg after a System.exit(), or if another finalise on the same thread throws an exception).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.