I have an abstract factory like so:
public abstract class DAOFactory {
public abstract EntryDAO<EntryDTO> getEntryDAO();
...
}
With my DAO and DTO interfaces like so:
public interface EntryDTO extends GenericDTO {
}
public interface EntryDAO<T extends EntryDTO> extends GenericDAO<T, Serializable> {
}
And my implementation like so:
public class EntryDTOImpl implements EntryDTO {
}
public class EntryDAOImpl<T extends EntryDTO> extends GenericDaoImpl<EntryDTOImpl, ObjectId>
implements EntryDAO<T> {
}
Now, if I create a factory and override the getEntryDAO method like so:
public class MyDAOFactory extends DAOFactory {
@Override
public EntryDAO<EntryDTO> getEntryDAO() {
try {
return new EntryDAOImpl<EntryDTOImpl>();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
I get a compile time error:
Type mismatch: cannot convert from EntryDAOImpl to EntryDAO
EDIT
I’ve updated my abstract factory like so:
public abstract <T extends EntryDTO> EntryDAO<T> getEntryDAO();
And made the change in my factory implementation:
@Override
public <T extends EntryDTO> EntryDAO<T> getEntryDAO() {
try {
return new EntryDAOImpl<EntryDTOImpl>();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Now I’m getting the compile time error:
Type mismatch: cannot convert from EntryDAOImpl<EntryDTOImpl> to EntryDao<T>
Define the abstract method this way (notice
?instead ofT):I recreated the scenario the way I understood it: returning
subclass1<subclass2>asinterface1<interface2>For example, this works well:
But this doesn’t work:
This gives the same compile-time error that you got:
Type mismatch: cannot convert from ArrayList<String> to List<B>