1

I was looking at Comparator<T> interface from java.util and I saw the fact that is annotated with @FunctionalInterface.

Can someone please explain me why a compile error is not present since two non default methods are present in the interface? The methods are:

int compare(T o1, T o2);

and

boolean equals(Object obj);
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Aditzu
  • 696
  • 1
  • 14
  • 25
  • 5
    Interface methods matching `public` methods declared in `java.lang.Object` do not count. This applies to `boolean equals(Object)`. – Holger Jan 06 '17 at 10:19
  • Thank you for your response. There is some documentation somewhere saying this? I'm quit surprised because is the first time when I hear this. – Aditzu Jan 06 '17 at 10:28
  • 1
    Refer : [java doc](https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html) paragraph 1 – Rohit Gulati Jan 06 '17 at 10:30
  • 1
    The formal specification is set in [JLS §9.8. Functional Interfaces](http://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.8) – Holger Jan 06 '17 at 10:31
  • Now I was thinking more about it... It makes sense since every functional interface is an object and already contains those methods :) – Aditzu Jan 06 '17 at 10:32
  • 1
    And the methods of a non-interface class like `java.lang.Object` always take precedence, so if you declare a `default boolean equals(Object obj) { … }` rather than an `abstract` method, the implementation class will still use the method inherited from `java.lang.Object`… – Holger Jan 06 '17 at 10:35
  • In this case what is the purpose of the equals method in Comparator interface? – Aditzu Jan 06 '17 at 10:43
  • 2
    To add a javadoc comment whose contents appears in the documentation of the `interface Comparator`. – Holger Jan 06 '17 at 11:41
  • Great answer Holger! Thank you! – Aditzu Jan 06 '17 at 12:17

0 Answers0