I am working on a project in which services should be added to the component. The Service class is an interface without any methods. Here is an example of how my services work:
public interface Service { } public interface CarWash extends Service { void washCar(Car car); } public interface CarRepair extends Service { void repairCar(Car car); }
Now there are many implementations of these services. One class can implement several services, since this class of garage:
public class Garage implements CarWash, CarRepair { @Override public void washCar(Car car) { } @Override public void repairCar(Car car) { } }
When adding a service to a component, I don’t want to use the service for all tasks, but, for example, use Garage only to wash cars ( CarWash ), but not to restore them ( CarRepair ). Therefore, I define tasks as classes, like this:
void addService(Service service, Class<? extends Service> task);
To check if the service could actually complete the task, I used generics:
<T extends Service> addService(T service, Class<? super T> task);
This works well, but does not check if the provided task is actually a task (a class that implements Service ), so this will work:
addService(myTask, Object.class);
I am looking for a way to indicate that Service needs to implement (extend) the task And task extends the Service interface , like this (not compilation):
<T extends Service> addService(T service, Class<? super T extends Service> task);