Why is the following piece of code not working?
import java.util.Comparator;
public class TestInner {
public static void main(String[] args) {
Comparator<String> comp = new Comparator<String>(){
private String sample = null;
@Override
public int compare(String arg0, String arg1) {
// TODO Auto-generated method stub
return arg0.compareTo(arg1);
}
public void setText(String t1){
sample = t1;
}
};
// compiler error - Method is undefined for the type Comparator<String>
comp.setText("xyz");
}
}
I have used Comparator just in the sample code. The same is happening with Comparator of any object.
I'm creating an inner class that has implemented the Comparator interface, why is it now allowing me to add new methods here?