title: Using instanceof to test interfaces
body: <p>I've been having a bit of trouble with this problem. The question:</p>
  
  <p>Write a complete Java program that does the following: </p>
  
  <ul>
  <li>declares interfaces I1 and I2, both with empty bodies</li>
  <li>declare interface I3 with an empty body, which extends both of the
  above interfaces</li>
  <li>declare interface I4 with an empty body</li>
  <li>class X implements I3 with an empty body</li>
  <li>class W with an empty body implements I4 and extends X</li>
  <li>create a class InstanceofTest that does the following in the main():
  <ul>
  <li>Create an object w of class W.</li>
  <li>Use the instanceof operator to test if the object w implements each
  of the interfaces and is of type X, and display the appropriate
  message.</li>
  </ul></li>
  </ul>
  
  <hr>
  
  <p>So this is what I've come up with so far: </p>
  
  <hr>
  
  <pre><code>public interface I1
  {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface I2
  {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface I3 extends I1, I2
  {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public interface I4
  {
  
  }
  </code></pre>
  
  <hr>
  
  <pre><code>public class W extends X implements I4
  {
  
  }
  </code></pre>
  
  <hr>
  
  <p>I'm having a bit of confusion with the InstanceofTest method. I know that the instanceof operator would tell you if a certain object is an instance of a certain type, like when you do it like this: </p>
  
  <hr>
  
  <pre><code>public class InstanceofTest
  {
  
      public static void main(String[] args)
      {
          W w = new W();
          if (w instanceof X)
          {
              System.out.println("w is an instance of X.");
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>The problem I'm having is using <code>instanceof</code> to see if w implements each of the interfaces. I have no idea how I would go about doing that. Any help would be greatly appreciated!</p>
  
  <hr>
  
  <p>Edit: So, should I do it like this?</p>
  
  <hr>
  
  <pre><code>public class InstanceofTest
  {
      public static void main(String[] args)
      {
          W w = new W();
          if (w instanceof X)
          {
              System.out.println("w is an instance of X.");
          }
  
          if (w instanceof I1)
          {
              System.out.println("w implements I1.");
          }
  
          if (w instanceof I2)
          {
              System.out.println("w implements I2.");
          }
  
          if (w instanceof I3)
          {
              System.out.println("w implements I3.");
          }
  
          if (w instanceof I4)
          {
              System.out.println("w implements I4.");
          }
      }
  }
  </code></pre>
  
  <hr>