What is wrong with this code?
public interface FileProccessor {
public <RT> RT setHeader(RT header);
}
public class AProcessor implements FileProccessor {
@Override
public Header setHeader(Header header) {
return null;
}
}
Compilator is complaining: The method setHeader(Header) of type AProcessor must override or implement a supertype method
Edit:
Thanks. I got confused because I wanted multiple methods with different types.
Now I realized a can add as many parametrized types as I want in class level. Like FileProcessor<T, F, M>
.
Your interface declaration specifies that all implementations of the interface will provide a generic method with this signature:
public <RT> RT setHeader(RT header);
In other words, the caller would be able to pass an object of any type, and get back an object of the same exact type.
You implementation declaration, on the other hand, limits the user to a single type, i.e. the Header
. That's why the compiler is complaining about inability to override.
The trick would work if the interface were generic, and a type implementing it was implementing a generic instance, like this:
public interface FileProccessor<T> {
public T setHeader(T header);
}
public class AProcessor implements FileProccessor<Header> {
}
I think you wanna do this:
public interface FileProccessor<RT, RX> {
public RT setHeader(RT header);
public RX setFooter(RX footer);
}
public class AProcessor implements FileProccessor<Header, Footer> {
@Override
public Header setHeader(Header header) {
return null;
}
@Override
public Footer setFooter(Footer footer) {
return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With