title: How do you substitute a variable in the "instanceof" function
body: <p>I have the following classes in my java project <code>Wheels</code>, <code>Engine</code>, <code>Brake</code> which all implement <code>CarPart</code></p>
  
  <p>Objects created are stored in an <code>ArrayList&lt;CarPart&gt; parts</code> variable.</p>
  
  <p>I want to check three things:</p>
  
  <ol>
  <li>Does my car have any Wheels?</li>
  <li>Does my car have any Engine?</li>
  <li>Does my car have any Brake?</li>
  </ol>
  
  <p>I can write the <code>hasWheels</code> function, then write literally the same code again but replace Wheels with <code>Engine</code>, then with <code>Brake</code>. Its all duplicate code. Is there a way where I can pass a parameter to a function like <code>doesArrayContain(parts, Wheels)</code>?</p>
  
  <hr>
  
  <pre><code>public boolean hasWheels(ArrayList&lt;CarPart&gt; parts) {
  
      for (CarPart part : parts) {
          if (part instanceof Wheels)
              return false;
      }
  
      return true;
  }
  </code></pre>
  
  <hr>
  
  <p>Something like this</p>
  
  <pre><code>public boolean doesArrayContain(ArrayList&lt;CarPart&gt; parts, Type type) {
  
      for (CarPart part : parts) {
          if (part instanceof type)
              return false;
      }
  
      return true;
  }
  </code></pre>