title: Java multi-type method parameter?
body: <p>I wonder if it is possible to require that a java method parameter is of any type from finite set of types. For example - I am using a library where two (or more) types have common methods, but their lowest common ancestor in the type hierarchy is Object. What I mean here:</p>
  
  <pre><code>   public interface A {
        void myMethod();
     }
  
     public interface B {
        void myMethod();
     }
  ...
     public void useMyMethod(A a) {
        // code duplication
     }
  
     public void useMyMethod(B b) {
        // code duplication
     }
  </code></pre>
  
  <p>I want to avoid the code duplication. What I think of is something like this:</p>
  
  <pre><code>   public void useMyMethod(A|B obj){
        obj.myMethod();
     }
  </code></pre>
  
  <p>There is similar type of syntax in java already. For example:</p>
  
  <pre><code>  try{
       //fail
    } catch (IllegalArgumentException | IllegalStateException e){
       // use e safely here
    }
  </code></pre>
  
  <p>Obviously this is not possible. How can I achieve well designed code using such type of uneditable type hierarchy ?</p>