When I write the method this way. I get this warning:
BaseEvent is a raw type. References to generic type BaseEvent
should be parameterized
@Override
public <T extends BaseEvent> void actionPerformed(T event) { ... }
The code still runs fine, although the warning sign is annoying. When I write the code this way the warning goes away.
@Override
public <T> void actionPerformed(BaseEvent<T> event) { ... }
With the previous message, It doesn’t guarantee that is a subClass of BaseEvent. So I changed it again:
@Override
public <T extends EventObject> void actionPerformed(BaseEvent<T> event) { ... }
@Override
public <T extends BaseEvent<T>> void actionPerformed(BaseEvent<T> event) { ... }
BaseEvent class is a class I made that extends EventOBject
public abstract class BaseEvent<T> extends EventObject
{
private String eventType;
// Constructor
public BaseEvent(Object source, String type)
{
super(source);
eventType = type;
}
public String getEventType() { return eventType; }
}
All the methods seem to work fine. But I was wondering which is the better solution.
Where do you use
TinBaseEventdefinition? Define it in the following waythen you won’t get a warning with
UPDATE
Suppose your
BaseEventreally required to be parametrized. Then write followingThis will give you a parametrized method.
UPDATE 1
It doesn't guarantee that is a subClass of BaseEvent.It does.
<T>is a parameter for method template. This parameter goes toBaseEvent<T>which is subclass ofEventObjectby definition.UPDATE 2
Do not use generics at the beginning of your learning. Generics are just for additional self testing. Use raw types. Then when you start to feel them, you will parametrize them correctly.