-2
class problemsolver implements Comparator<Student> {

   public int compare(Student obj1,Student obj2) {
       return obj1.fname.compareTo(obj2.fname);
   } 
}

When I write simply class problemsolver implements Comparator it is showing this error.

prog.java:25: error: problemsolver is not abstract and does not override abstract method compare(Object,Object) in Comparator

Why?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Dj15
  • 13
  • 4
  • `Comparator` or `Comparator` requires a signature `compare(Object o1, Object o2)` – Andrew Tobilko Jun 13 '18 at 08:49
  • Because Comparator has a generic type parameter, and leaving that out is wrong. Learn about generics, in while doing so, you also learn why to not leave them out. – GhostCat Jun 13 '18 at 08:50

1 Answers1

2

Implicitely Comparator is Comparator<Object>, then compiler expects you to implements compare(Object, Object).

What you need is :

class problemsolver implements Comparator<Student>
yunandtidus
  • 3,847
  • 3
  • 29
  • 42