I am trying to implement this code:
public void ActionTypeOne {
doSomething(ActionType1A action) { ... }
doSomething(ActionType1B action) { ... }
}
...
public void register(Action action) {
if (action.getClass().equals(ActionType1A) || action.getClass().equals(ActionType1b)) {
ActionTypeOne.doSomething(action);
}
}
Since ActionTypeOne does not implement doSomething(Action), but only doSomething(ActionType1A) and doSomething(ActionType1B), this code will not compile.
However, I know that my object is definitely one of those types because of the code beforehand. I could split this up and cast explicitly to one of my two ActionTypes, but that is messy.
Can I force the line:
ActionTypeOne.doSomething(action)
to call the correct method in ActionTypeOne based on the class of action?