0

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.

3 Answers 3

2

Where do you use T in BaseEvent definition? Define it in the following way

public abstract class BaseEvent extends EventObject 

then you won't get a warning with

@Override public void actionPerformed(BaseEvent event) { ... } 

UPDATE

Suppose your BaseEvent really required to be parametrized. Then write following

@Override public <T> void actionPerformed(BaseEvent<T> event) { ... } 

This 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 to BaseEvent<T> which is subclass of EventObject by 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.

Sign up to request clarification or add additional context in comments.

2 Comments

You should mention that what you have in the first piece of code public void actionPerformed(BaseEvent event) { ... } works the exact same way as what he has in his first piece of code public <T extends BaseEvent> void actionPerformed(T event) { ... }. Both take any instance of BaseEvent, which of course includes subclasses of BaseEvent.
Hey Dims, thanks for the explanation. I took out the type parameter from BaseEvent. And thanks for clarifying how to use it when needed.
1

The type parameter T is never used in the class definition. You might be able to remove the type parameter from BaseEvent:

public abstract class BaseEvent extends EventObject { ... } 

and just define your method without a type parameter:

@Override public void actionPerformed(BaseEvent event) { ... } 

1 Comment

Your right about the class never using T. I removed it and it works fine. Plus looks a lot cleaner
1

The best solution is the one that avoids the warnings and guarantees your type safety at compile time.

If the type of T in BaseEvent doesn't matter, couldn't you just use your first one and parameterize BaseEvent? Do something like:

@Override public <T extends BaseEvent<?>> void actionPerformed(T event) { ... } 

Alternatively, it looks like your BaseEvent class does not actually use T for anything - why is it there?

1 Comment

Your its not needed. So I removed it. Originally I thought it needed to be there so I can keep track the type of event.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.