Answer: For sorting employees we need to use either Comparable or comparator interface. Since, as per question, the Employee class is not allowed to implement any Interface, only option is, to create a new comparator class that implements Comparator interface.
And in “compare()” method of comparator class, write the comparison code for the employee class on the basis of experience.
Sample:
//Class
public class Employee {
private String name;
private int experience;
public Employee(String name, int experience) {
this.name = name;
this.experience = experience;
}
public int getExperience() {
return experience;
}
@Override
public String toString() {
return "Employee Name=" + this.name + ", Experience=" + this.experience;
}
}
//Comparator class
public class ComparatorByExperience implements Comparator