I have a class Employee and a class Building.
These classes are not related to each other from class hierarchy perspective.
I need to process a bunch of Employee and Building objects and their results will end up in different lists.
So I have and interface e.g.
public interface Processor{
public void process(String s, List<?> result);
}
The idea is that the string s carries information related to either an Employee or a Building and the implementation after some processing adds to the result list either an Employee object or a Building object.
There are 2 implementations of Processor one is EmployeeProcessor and a BuildingProcessor.
Somewhere in the code I get a reference to either of these and I pass either a List<Employee> or a List<Building> to the process method.
Problem is that the code does not compile.
When I do e.g. inside the EmployeeProcessor : result.add(new Employee(a,b,c,d));
I get:
The method add(capture#2-of ?) in the type List is not
applicable for the arguments
I guess I can understand the problem, but I don’t want to change the interface to:
public interface Processor{
public process(String s, List result);
}
I.e. not specify type of list.
Is there a way around this problem? Is the interface definition wrong?
Note: this interface is part of implementation of Command pattern
This is what I was thinking.
FYI, you can further limit the type. For example, if both
BuildingandEmployeeclasses implement, lets say,Processable, then you can dointerface Processor<T extends Processable>