In java we cannot override a final method but is it possible to overload ?
A:
Yes It is possible.
You can examine this kind of things by writing small Java classes.
class TestClass{
final public void testMethod(){
}
final public void testMethod(int i){
}
}
Upul
2010-01-01 07:04:18
+3
A:
Yes, overloading a final method is perfectly legitimate.
For example:
public final void doStuff(int x) { ... }
public final void doStuff(double x) { ... }
Taylor Leese
2010-01-01 07:05:18
+2
A:
YES, why not? It is as legal as overloading non-final methods.
But you cannot override them (you already know it).
Eg :
public final void func(String x) {/* code */}
public final void func(double x) { /* more code */ }
public final void func(int x) { /* yeah I have still more code */ }
Prasoon Saurav
2010-01-01 07:06:10
Why one down vote?
Prasoon Saurav
2010-01-01 10:29:29
A:
yes overloading final method is possible in java.As final methods are restricted not to override the methods.
while overloading argument list must be different type of the overloading method.
giri
2010-01-01 07:16:53
+3
A:
Yes, very much possible.
A small program to demonstrate it:
class A{
final void foo(){ System.out.println("foo ver 1 from class A"); }
final void foo(int a){ System.out.println("foo ver 2 from class A"); }
}
class B extends A{
final void foo(long l){ System.out.println("foo ver 3 from class B"); }
// final void foo(){ System.out.println("foo ver 1 from class A"); } NOT ALLOWED
}
public class Test {
public static void main(String [] args){
B obj = new B();
obj.foo();
obj.foo(1);
obj.foo(1L);
}
}
Output:
foo ver 1 from class A foo ver 2 from class A foo ver 3 from class B
gameover
2010-01-01 07:23:33
+1
A:
Yes, but be aware that dynamic dispatch might not do what you are expecting! Quick example:
class Base {
public final doSomething(Object o) {
System.out.println("Object");
}
}
class Derived extends Base {
public 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
ZoFreX
2010-01-01 12:44:50