I am implementing Comparator and Comparable in my class Employee to sort an arraylist. I am sorting on 2 parameters name and ID. While sorting with ID using Collections.sort(Employee ob1, Employee ob2) it doesn't work. I get the compilation error that the class is not declared abstact and i am not overriding compare(), although i am doing it as:
public int compare(Employee ob1, Employee ob2)
{
return ob1.id-ob2.id;
}
it works fine if i use
public int compare(Object ob1, Object ob2)
{
return ((Employee)ob1).id-((Employee)ob2).id;
}
My doubt is why does't the earlier one work since Employee is also an Object. The same issue is with compareTo(Object ob) as well, While overriding why can't i use Employee directly and why do i need to use Object there and later need to cast it to Employee class.