title: Constructor for Comparator Interface in Java
body: <p>I know that an interface cannot have constructors and we cannot make object of interface.</p>
  
  <p>This is not possible:</p>
  
  <pre><code>Comparator cmp = new Comparator(); 
  </code></pre>
  
  <p>I don't understand how is it possible to make an anonymous inner class with the keyword "new Comparator()". Doesn't this keyword create an object of the type Comparator? </p>
  
  <p>Here is the complete code:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  
  public class DemoApp {
  
      public static void main(String args[]) {
  
          List&lt;String&gt; animals = new ArrayList&lt;&gt;();
          animals.add("elephant");
          animals.add("snake");
          animals.add("lion");
          animals.add("mangoose");
          animals.add("cat");
          animals.add("tiger");
  
          Collections.sort(animals, new Comparator&lt;String&gt;() {
              public int compare(String s1, String s2) {
                  return -s1.compareTo(s2);
              }
          });
          displayList(animals);
      }
  
      public static void displayList(List&lt;String&gt; anim) {
          for (String animal : anim) {
              System.out.print(animal + " "); 
          }
          System.out.println();
      }
   }
  </code></pre>