title: Most efficient way to rule out classes from an interface's method
body: <p>Up to this point, I have used the following code to restrict the application of some methods to instances of certain classes. For instance, using <code>ItemListener</code>, but this could be applied to many things,</p>
  
  <pre><code>public class mListener implements ItemListener {
      public void itemStateChanged(ItemEvent e) {
  
          if (!(e.getItemSelectable instanceof JCheckBox)) { //again, JCheckBox was chosen arbirtarily
              System.err.println("mListener can only be applied to a JCheckBox");
              return;
          }
  
      }
  }
  </code></pre>
  
  <p>However, in a few places on the Oracle Java tutorials, I have seen the following code</p>
  
  <pre><code>public class mListener implements ItemListener {
      public void itemStateChanged(ItemEvent e) {
          JCheckBox box = null;
  
          try {
              box = (JCheckBox) e.getItemSelectable();
          } catch (ClassCastException ex) {
              System.err.println("mListener can only be applied to a JCheckBox");
              return;
          }
  
      }
  }
  </code></pre>
  
  <p>Which is the best way to lock out classes that you don't want a method applied to? This is especially the case with implementing interfaces, where the parameter can't be changed.</p>