Declare parameter subtypes in Java interfaces and use subtypes in Java implementation methods
•
Java
I want to declare a method in the interface, where the parameters of the method defined in the implementation class can be subtypes of a specific Java class, for example:
interface Processor{
processRequest( Request r);
}
public class SpecialRequest extends Request{...}
public class SpecialProcessor implements Processor{
processRequest(SpecialRequest r){...}
}
But I encountered an error in specialprocessor because it did not implement the processor interface correctly What can I change in the processor interface to allow definitions in specialprocessor to work?
Solution
You can enter the processor:
public interface Processor<R extends Request> {
void processRequest(R r);
}
public class SpecialProcessor implements Processor<SpecialRequest> {
public void processRequest(SpecialRequest r) {
...
}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
