So I asked this question before but I had a mistake in the code which most people picked up on, rather than the problem itself.
Anyway, I'm trying to override an interface method in a class. However, I want the type of the parameter in the overriding method to be a subclass of the type of the parameter as defined in the overriden method.
The interface is:
public interface Observer {
public void update(ComponentUpdateEvent updateEvent) throws Exception;
}
While the class that overrides this method is:
public class ConsoleDrawer extends Drawer {
//...
@Override
public void update(ConsoleUpdateEvent updateEvent) throws Exception {
if (this.componentType != updateEvent.getComponentType()) {
throw new Exception("ComponentType Mismatch.");
}
else {
messages = updateEvent.getComponentState();
}
}
//...
}
ConsoleUpdateEvent is a subclass of ComponentUpdateEvent.
Now, I could just have the update() method in ConsoleDrawer take a ComponentUpdateEvent as a parameter and then cast it to a ConsoleUpdateEvent but I'm looking for a slightly more elegant solution if possible. Anyhelp would be appreciated. Thank you.