title: Passing object of multiple types as parameter to a method
body: <p>Since Java 7, we can catch multiple exceptions in the same catch clause like the following.</p>
  
  <pre><code>try { 
    ...
  } catch( IOException | SQLException ex ) { 
    ...
  }
  </code></pre>
  
  <p>Similarly, Is there any way to implement like the following without using Inheritance?</p>
  
  <pre><code>public void passMultipleTypes(Type1 | Type2 obj) {
    ...
  }
  </code></pre>
  
  <p>The <code>obj</code> object can either be <code>Type1</code> or <code>Type2</code>. <strong>I do not want to use inheritance here as these classes are generated and I cannot change them</strong>. So I cannot define them as </p>
  
  <pre><code>public class Test1 extends CommonSuperClass {
    ...
  }
  </code></pre>
  
  <p><code>Type1</code> and <code>Type2</code> have similar attributes. So I was thinking of working with <code>obj</code> like the following.</p>
  
  <pre><code>public void passMultipleTypes(Type1 | Type2 obj) {
    System.out.println(obj.getCode());
    System.out.println(obj.getValue());
  }
  </code></pre>