The following code is considered invalid by the compiler:
class Foo {
void foo(String foo) { ... }
}
class Bar extends Foo {
@Override
void foo(Obje
If you can override with superclasses, why not with subclasses as well?
Consider the following:
class Foo {
void foo(String foo) { ... }
}
class Bar extends Foo {
@Override
void foo(Object foo) { ... }
}
class Another extends Bar {
@Override
void foo(Number foo) { ... }
}
Now you have succesfully overriden an method whose original parameter was a String
to accept a Number
. Inadvisable to say the least...
Instead, the intended results may be replicated by using overloading and the following, more explicit, code:
class Foo {
void foo(String foo) { ... }
}
class Bar extends Foo {
@Override
private void foo(String foo) { ... }
void foo(Object foo) { ... }
}
class Another extends Bar {
@Override
private void foo(Object foo) { ... }
void foo(Number foo) { ... }
}