Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to overload a final method

Tags:

java

In java we cannot override a final method but is it possible to overload ?

like image 937
GuruKulki Avatar asked Jan 01 '10 07:01

GuruKulki


2 Answers

Yes, overloading a final method is perfectly legitimate.

For example:

public final void doStuff(int x) { ... }
public final void doStuff(double x) { ... }
like image 103
Taylor Leese Avatar answered Sep 28 '22 12:09

Taylor Leese


Yes, but be aware that dynamic dispatch might not do what you are expecting! Quick example:

class Base {
    public final void doSomething(Object o) {
        System.out.println("Object");
    }
}

class Derived extends Base {
    public void doSomething(Integer i) {
        System.out.println("Int");
    }
}

public static void main(String[] args) {
    Base b = new Base();
    Base d = new Derived();
    b.doSomething(new Integer(0));
    d.doSomething(new Integer(0));
}

This will print:

Object
Object
like image 30
ZoFreX Avatar answered Sep 28 '22 13:09

ZoFreX