4

I've read somewhere that every method can be overloaded.

finalize()

is a method of course. But while searching I also found that you cannot overload this method.

So the Question is Can we overload the finalize() method? If yes then how is it implemented?

Raúl
  • 1,542
  • 4
  • 24
  • 37
Saif
  • 305
  • 2
  • 12
  • I think the question is about `finalize()`, not `final`. – Sridhar Apr 21 '15 at 13:45
  • How can it be overloaded if it doesn't accept any arguments ? or did you mean overrided ? why don't you simply try to do it and see what happens ? – Nir Alfasi Apr 21 '15 at 13:45
  • 3
    @alfasin For the record, though, a method that doesn't accept any arguments can easily be overloaded -- just write a method of the same name, in the same class, that _does_ take arguments. – yshavit Apr 21 '15 at 13:46
  • 2
    First things first: why use `finalize()` at all? – fge Apr 21 '15 at 13:47
  • This question is about `Object#finalize` (which is not final) not about final method. – Jean-François Savard Apr 21 '15 at 13:48
  • 1
    "while searching I also found that you cannot overload this method" <-- source, please? – fge Apr 21 '15 at 13:49
  • @yshavit you're saying the obvious, but in our particular case - will such a method be called upon object destruction ? I should have probably been clearer with my wording - but the purpose is altogether the same. – Nir Alfasi Apr 21 '15 at 14:43
  • @alfasin Well, it's an important distinction. No, such a method will not be called upon finalization, because (and this is crucial) overloaded methods have _no_ relationship to one another (other than being similarly named and in the same class). In other words, while overriding is something the JVM has to worry about, overloading is something that _only_ concerns a human brain, which (erroneously) thinks, "oh, these two methods are connected." Two methods `foo()` and `foo(int)` have exactly the same connection as two methods `fiz()` and `baz(int)`. – yshavit Apr 21 '15 at 14:51

5 Answers5

7

Yes, you can. But the overloaded version won't be called by the JVM.

@Override
protected void finalize() throws Throwable
{
  super.finalize();
}

protected void finalize(String lol) throws Throwable
{
  finalize();
}  
Evan Knowles
  • 7,426
  • 2
  • 37
  • 71
4

You can overload the finalize method, this would compile successfully :

public class Test
{
    public void finalize(String hi)
    {
        System.out.println("test");
    }
}

However it won't be called by the Garbage collector and JVM as it will call the one with no parameters.

GC and JVM will only and always call the finalize with the signature

protected void finalize() throws Throwable

Workaround

You could simply override finalize and make it call the method with parameters :

public class Test
{
    public static void main(String args[])
    {
        new Test();
        System.gc();
    }

    @Override
    public void finalize() throws Throwable
    {
        super.finalize();
        this.finalize("allo");
    }

    public void finalize(String hi)
    {
        System.out.println(hi);
    }
}

That would print allo. That way you could call finalize(String hi) where you want, and the GC would also call it as you overrided finalize() to call it.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
2

Method Overloading is done in same & child class, while Overriding is done in child class only. We do override other Object class methods (equals(), hashCode() etc.) due to their significance, but never overload any of these methods.

So while you CAN overload the finalize() method, it would be just like a new method in your child class, and wouldn't be considered by JVM (as a finalize() method really).

Raúl
  • 1,542
  • 4
  • 24
  • 37
  • 1
    a method overloading can be done in a class that inherits from a base class. As for instance the finalize method is protected and defined on the Object class, therefore if I define any other method called finalize with a different signature from the base finalize method in any java class I have indeed overloaded the base finalize method – alainlompo Apr 21 '15 at 13:59
1

You can overload the finalize method without any issue. Here is another example:

public class FinalizeOverloader {

    protected void finalize(String userId) {
        System.out.println("I could overload finalize...");
    }

    public static void main(String[] args) {
        FinalizeOverloader fO = new FinalizeOverloader();
        fO.finalize("AbC");
    }

}
alainlompo
  • 4,414
  • 4
  • 32
  • 41
0

While overloading finalize() is possible just like any other method, I don't see the point to do that. JVM will not invoke that method and even if this is not your goal, overloading it would just generate confusion.

If you meant overriding on the other hand, it could make more sense but it is strongly a not recommended approach. There are many downsides of using finalizers. For more info you can find some good advice (Item 7) in Joshua Bloch's "Effective Java" book. It basically says, avoid using finalizers.

Hakan
  • 756
  • 1
  • 8
  • 17