I'm having some trouble in the following code.
public ArrayList<? extends IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
ArrayList<? extends IEvent> returnedEvents = new ArrayList<GoogleEvent>();
returnedEvents.add(new GoogleEvent());
return (returnedEvents);
}
This return the following compilation error for the "returnedEvents.add(new GoogleEvent()); line of code":
The method add(capture#1-of ? extends IEvent) in the type ArrayList is not applicable for the arguments (GoogleEvent)
The declaration of the class GoogleEvent is as follows:
public class GoogleEvent implements IEvent {...}
I know there are some tricky parts using generics in Java, thus the wild-cards but I can't seem to figure this out.
Thanks.
Why don't you write:
public List<IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime)
{
List<IEvent> returnedEvents = new ArrayList<IEvent>();
returnedEvents.add(new GoogleEvent());
return returnedEvents;
}
You don't need to use ? extends IEvent because by using only IEvent, Java will dynamically bind the GoogleEvent class. It's polymorphism.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With