I have an interface called Employee
and two implementing classes FullTimeEmployee
and PartTimeEmployee
. In a seperate class, I am attempting to use ObjectMapper
to assign the correct class. The method looks as follows
public Employee retrieveEmployee(String employeeId){
Employee employee;
var jsonString = lookupEmployee(employeeId);
employee = new ObjectMapper().readValue(jsonString, isFullTime(employeeId) ? FullTimeEmployee.class : PartTimeEmployee.class;
return employee;
}
This gives me a compiler error in the IDE where the Required Type is Class <capture of ? extends Employee>
, but provided is Class <FullTimeEmployee>
However, if I open the ternary up into a standard if...then
, everything works fine.
if (isFullTime(employeeId)){
employee = new ObjectMapper().readValue(jsonString, FullTimeEmployee.class);
} else {
employee = new ObjectMapper().readValue(jsonString, PartTimeEmployee.class);
}
What is happening here, and why does one work but not the other?