Purpose of Overriding a finalize Method in Java



In Java, overriding is a technique used to provide a specific implementation of a method that is already defined in a superclass (i.e., parent class), allowing a subclass (i.e., child class) to modify or extend the behavior of that method.

Purpose of Overriding the finalize() Method

The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected.

If we are overriding this method, then we need to call the finalize() method explicitly. The finalize() method can be invoked only once by the JVM or any given object.

Let's quickly discuss the finalize() method before proceeding with examples:

The finalize() Method

In Java, the finalize() method is a pre-defined method in the Object class, and it is declared as protected. It is called by the garbage collector before an object is removed from memory to allow cleanup operations.

Following is the syntax of the finalize() method:

protected void finalize() throws Throwable

Example 1

This example shows how the finalize() method can be overridden to perform cleanup actions before an object is collected by the garbage collector. 

public class FinalizeMethodTest {
   protected void finalize() throws Throwable {
      try {
         System.out.println("Calling finalize() method of FinalizeMethodTest class");
      } catch(Throwable th) {
         throw th;
      } finally {
         System.out.println("Calling finalize() method of Object class");
         super.finalize();
      }
   }
   public static void main(String[] args) throws Throwable {
      FinalizeMethodTest test = new FinalizeMethodTest();
      String str = "finalize() method in Java";
      str = null;
      System.out.println(str);
      test.finalize();
   }
}

The above program produces the following output:

null
Calling finalize() method of FinalizeMethodTest class
Calling finalize() method of Object class

Example 2

This is another example showing the purpose of overriding the finalize() method to perform custom cleanup before an object is garbage collected. Here, finalize() is called manually for obj1, and System.gc() is used to request garbage collection for obj2:

public class FinalizeExampleTest {
   int id;
   FinalizeExampleTest(int id) {
      this.id = id;
   }
   
   protected void finalize() throws Throwable {
      System.out.println("Object with id " + id + " is garbage collected");
   }

   public static void main(String[] args) throws Throwable {
      FinalizeExampleTest obj1 = new FinalizeExampleTest(101);
      FinalizeExampleTest obj2 = new FinalizeExampleTest(102);

      obj2 = null;
      obj1.finalize();

      System.gc(); //garbage collector
   }
}

Following is the output of the above program:

Object with id 101 is garbage collected
Object with id 102 is garbage collected
Updated on: 2025-05-27T15:07:58+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements