3

According to definition of functional interface - A functional interface is an interface that contains only one abstract method.

But Comparator<T> has two abstract methods:

int compare(T o1, T o2);

boolean equals(Object obj);

others are default and static.

JavaDocs mentions it as functional interface. How can it be?

Michael
  • 41,989
  • 11
  • 82
  • 128
Jashan Chahal
  • 291
  • 2
  • 8
  • I actually prefer the answers given here, though, perhaps they should be merged to the dupe? – Hulk Aug 26 '20 at 12:58

3 Answers3

8

You're reading the wrong definition, or at least, an (over)simplified one.

The proper definition of a FunctionalInterface is:

A functional interface is an interface that has just one abstract method (aside from the methods of Object), and thus represents a single function contract. This "single" method may take the form of multiple abstract methods with override-equivalent signatures inherited from superinterfaces; in this case, the inherited methods logically represent a single method.

SOURCE: Java Language Specification section 9.8

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
4

If you look at the source code of Comparator<T> , it is like below:

@FunctionalInterface
public interface Comparator<T> {

  // abstract method
  int compare(T o1, T o2);

  // abstract method, overriding public methods of `java.lang.Object`, so it does not count
  boolean equals(Object obj);

}

The equals is an abstract method overriding one of the public methods of java.lang.Object, this doesn’t count as an abstract method.

So In fact The Comparator only has one abstract method i.e int compare(T o1, T o2), and it meet the definition of functional interface.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
3

equals is only explicitly included in the Comparator interface so that they can add some additional JavaDocs to it, e.g. some comparator-specific requirements:

this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator

Source: Comparator JavaDocs

The JLS says:

interfaces do not inherit from Object, but rather implicitly declare many of the same methods as Object (§9.2)

So all the Comparator authors have done is declare something explicitly that is normally implicit.

If the methods of Object counted as abstract methods for the purposes of defining a functional interface then no functional interface would have just a single abstract method. As such, they are not considered:

A functional interface is an interface that has just one abstract method (aside from the methods of Object)

JLS, emphasis mine

Michael
  • 41,989
  • 11
  • 82
  • 128
  • So, is equals an abstract method or just like default method? – Jashan Chahal Aug 26 '20 at 12:58
  • @JasHanChaHal It is an abstract method, just not one that is considered as part of determining functional interface status. Updated my answer – Michael Aug 26 '20 at 12:59
  • The declaration on the interface is abstract, it wouldn't make sense to add a default implementation - that would never be used, because the `equals` method in `Object` is not abstract. – Hulk Aug 26 '20 at 13:04
  • 1
    @Hulk Indeed, and it's prohibited: "*It is a compile-time error if a default method is override-equivalent with a non-private method of the class Object, because any class implementing the interface will inherit its own implementation of the method*" – Michael Aug 26 '20 at 13:06