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??

Recommended Answers

All 4 Replies

Have you tried writing a test program to see what will happen?

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.

see
"
# 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.

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).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.