I want to override a method that has an parameter of type Object
:
public void setValue(Object value) {
// ...
}
and make that parameter to have a generic type T
:
@Override
public void setValue(T value) {
super.setValue(value);
}
How can I do this in Java?
In Eclipse I get these errors:
Multiple markers at this line
- The type parameter T is hiding the type T
- Name clash: The method setValue(T) of type TextField<T> has the
same erasure as setValue(Object) of type JFormattedTextField but does not
override it
- The method setValue(T) of type TextField<T> must override or
implement a supertype method
You can't make an overriding method accept a narrower type than the method it overrides.
If you could, the following would be possible:
class A {
public setValue(Object o) {…}
}
class B<T> extends A {
@Override
public setValue(T o) {…};
}
A a = new B<String>(); // this is valid
a.setValue(new Integer(123)); // this line would compile, but make no sense at runtime