title: Why Java interface can be instantiated in these codes?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/7586277/creating-an-object-of-an-interface">Creating an &ldquo;object&rdquo; of an interface</a>  </p>
  </blockquote>
  
  
  
  <p>I am new to Java. Based on my understanding:</p>
  
  <ul>
  <li>We cannot instantiate an <code>Interface</code>. We can only instantiate a <code>class</code> which implements an <code>interface</code>.</li>
  <li>The <code>new</code> keyword is used to create an object from a class.</li>
  </ul>
  
  <p>However, when I read the source codes of some Java programs, I found that sometimes an Interface is instantiated. For example:</p>
  
  <p><strong>Example 1:</strong></p>
  
  <pre><code>JButtonObject.addActionListener(new ActionListener()
  {
      public void actionPerformed(ActionEvent e)
      {
          //codes
      }
  });
  </code></pre>
  
  <p><strong>Example 2:</strong></p>
  
  <pre><code>SwingUtilities.invokeLater(new Runnable()
  {
      public void run()
      {
          //codes
      }
  });
  </code></pre>
  
  <p>In the example at above, ActionListener and Runnable are both Java interface. May I know why they can be instantiated in these codes?</p>
  
  <p>What is the purpose of instantiating an Interface? Refer to <a href="http://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html" rel="nofollow noreferrer">this example</a>, it seems that we should create an instance of a class which implement the interface.</p>