How to remove instanceof for calling the correct method?
<p>I have the following hierarchy of classes:</p>

<pre><code>public interface Message
public interface V2Message extends Message
public interface V3Message extends Message
</code></pre>

<p>I defined another interface to validate a message. </p>

<pre><code>public interface Validation {
  boolean validate(Message message);
}
</code></pre>

<p>Each class that implements <code>Validation</code> can deal with V2 and/or V3 messages, so in my implementation I have to distinguish the kind of message I want to validate because the code will be different.</p>

<pre><code>public class MyValidation implements Validation {
  public boolean validate(Message message) {
    if(message instanceof V2Message) {
      return validateV2((V2Message)message);
    } else if (message instanceof V3Message) {
      return validateV3((V3Message)message);
    }
}
</code></pre>

<p>I was wondering if there is a way to remove the use of <code>instanceof</code>.</p>
