I have an abstract Class Monitor.java which is subclassed by a Class EmailMonitor.java.
The method:
public abstract List<? extends MonitorAccount> performMonitor(List<? extends MonitorAccount> accounts)
is defined in Monitor.java and must be overridden in EmailMonitor.java.
I currently have the method overridden in EmailMonitor.java as follows:
@Override public List<EmailAccount> performMonitor(List<EmailAccount> emailAccounts) { //...unrelated logic return emailAccounts; }
However, this produces the compile time error:
Name clash: The method performMonitor(List<EmailAccount>) of type EmailMonitor has the same erasure as performMonitor(Lis<? extends MonitorAccount> emailAccounts) of type Monitor but does not override it
EmailAccount is a subclass of MonitorAccount, so (in my mind at least) overriding it in this way makes perfect sense. Seeing as the compiler is not happy with my logic though, How should I go about this correctly while still keeping my compile time checks to make sure that all calls to EmailMonitor.performMonitor() receive Lists of EmailAccount rather than some other type of MonitorAccount?
No, it’s not overriding it properly. Overriding means you should be able to cope with any valid input to the base class. Consider what would happen if a client did this:
There’s nothing in there which should give a compile-time error given your description – but it’s clearly wrong.
It sounds to me like
Monitorshould be generic in the type of account it can monitor, so yourEmailMonitorshould extendMonitor<EmailAccount>. So:You might want to think carefully about the generics in the
performMonitorcall though – what’s the return value meant to signify?