title: Uncompilable source code when using Comparator in Java
body: <p>I'm getting the following error when trying to run this little program: </p>
  
  <pre><code>Exception in thread "main" java.lang.ExceptionInInitializerError
      at MainClass.main(Person.java:120)
  Caused by: java.lang.RuntimeException: Uncompilable source code - FirstNameComparator is not abstract and does not override abstract method compare(java.lang.Object,java.lang.Object) in java.util.Comparator
      at FirstNameComparator.&lt;clinit&gt;(Person.java:71)
      ... 1 more
  Java Result: 1
  </code></pre>
  
  <p>Please tell me what I'm doing wrong, and of course it would help a short example with explanations about how to properly use <code>Comparator</code> and <code>Comparable</code>.</p>
  
  <pre><code>import java.util.*;
  import java.lang.*;
  
  public class Person implements Comparable {
  
      private String firstName;
      private String lastName;
      private int age;
  
      public String getFirstName() {
          return firstName;
      }
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
      public void setLastName(String lastName) {
          this.lastName= lastName;
      }
  
      public String getLastName() {
          return lastName;
      }
  
      public void setAge(int age) {
          this.age=age;
      }
      public int getAge() {
          return age;
      }
  
      @Override
      public int compareTo(Object anotherPerson) throws ClassCastException {
          if(!(anotherPerson instanceof Person)) {
              throw new ClassCastException("A person object expected !");
          }
          int anotherPersonAge = ((Person)anotherPerson).getAge();
          return this.age = anotherPersonAge;
      }
  
  }
  </code></pre>
  
  <p>LastNameComparator.java</p>
  
  <pre><code>class LastNameComparator implements Comparator  {
  
      public int compareTo(Object person, Object AnotherPerson)  {
          String lastName1 = ((Person)person).getLastName().toUpperCase();
          String firstName1= ((Person)person).getFirstName().toUpperCase();
          String lastName2 = ((Person)person).getLastName().toUpperCase();
          String firstName2 = ((Person)person).getFirstName().toUpperCase();
  
          if(lastName1.equals(lastName2)) {
              return firstName1.compareTo(firstName2);
  
          }else {
             return lastName1.compareTo(lastName2);
  
          }
      }
  
      }   
  </code></pre>
  
  <p>FirstNameComparator.java</p>
  
  <pre><code>class FirstNameComparator implements Comparator {
  
      public int compareTo(Object person, Object anotherPerson) {
  
          String lastName1 = ((Person)person).getLastName().toUpperCase();
          String firstName1 = ((Person)person).getFirstName().toUpperCase();
          String lastName2 = ((Person) person).getLastName().toUpperCase();
          String firstName2 = ((Person) person).getFirstName().toUpperCase();
  
          if(firstName1.equals(lastName1)) {
              return lastName1.compareTo(lastName2); 
  
          }else {
              return firstName1.compareTo(firstName2);
          }
  
      }
  }
  </code></pre>
  
  <p>MainClass.java</p>
  
  <pre><code>class MainClass  {
  
      public static void main(String[] args) {
  
          Person[] persons = new Person[4];
          persons[0] = new Person();
          persons[0].setFirstName("Asaftei");
          persons[0].setLastName("Ionut");
          persons[0].setAge(25);
  
          persons[1] = new Person();
          persons[1].setFirstName("Binoclu");
          persons[1].setLastName("Telescop");
          persons[1].setAge(10);
  
          persons[2] = new Person();
          persons[2].setFirstName("Morcov");
          persons[2].setLastName("Castravete");
          persons[2].setAge(33);
  
          persons[3] = new Person();
          persons[3].setFirstName("Rosie");
          persons[3].setLastName("Ardei");
          persons[3].setAge(55);
  
          System.out.println("Natural Order : ");
          for(int counter =0; counter&lt;=3; counter++)  {
              Person person = persons[counter];
              String lastName=person.getLastName();
              String firstName=person.getFirstName();
              int age = person.getAge();
              System.out.println(lastName+ ", " +firstName+ " , "+age);
          }
  
          Arrays.sort(persons, new FirstNameComparator());
          System.out.println();
          System.out.println("Sorted by First Name: ");
          for(int counter=0; counter&lt;=3; counter++) {
              Person person = persons[counter];
              String lastName = person.getLastName();
              String firstName = person.getFirstName();
              int age = person.getAge();
  
              System.out.println(lastName+ ", " +firstName+ ", " +age );
          }
          Arrays.sort(persons, new LastNameComparator());
          System.out.println();
          System.out.println("Sorted by Last Name");        for(int counter=0; counter&lt;=3; counter++) {
              Person person = persons[counter];
              String lastName = person.getLastName().toUpperCase();
              String firstName = person.getFirstName().toUpperCase();
              int age = person.getAge();
              System.out.println(lastName +", "+firstName+", "+age);
          }
      }
  }
  </code></pre>