Example code:
public abstract class AbstractEvent
{
// extended by AbstractEvent subclasses, used as a lambda function interface
// (all sub-interfaces define one method)
public interface EventListener
{
}
// assigned by subclasses in a static block
protected static Class<? extends EventListener> listenerClass;
// this line obviously does not work
protected static Set<listenerClass> listeners = new HashSet<>();
public final boolean addListener(listenerClass listener)
{
return listeners.add(listener);
}
public final boolean removeListener(listenerClass listener)
{
return listeners.remove(listener);
}
}
Is something like this even possible in Java, and if so, how? I've made it in Scala by defining a type without a body in the abstract class and defining it in subclasses (and it works great), but I'd like to make it in Java aswell.
The point of this is that I want to avoid duplicating the set code in all subclasses of AbstractEvent.