4

According to Java Language Specification (version 8.0), “interfaces do not inherit from Object, but rather implicitly declare many of the same methods as Object.” If you provide an abstract method from Object class in the interface, it still remains a functional interface.

@FunctionalInterface
interface EqualsInterface {
    boolean equals(Object obj);
}

The compiler gives the error: “ EqualsInterface is not a functional interface: no abstract method found in interface EqualsInterface ”. Why? Thanks in advance

silvia Dominguez
  • 445
  • 1
  • 4
  • 16
  • 8
    see here: https://stackoverflow.com/a/14656085/3959856. your equals is excluded and the compiler sees zero abstract methods. change the name of your method.. doesEqual maybe :) – Jack Flamp Aug 22 '18 at 12:29
  • Rename `equals` to `isEqualTo` and you have your abstract method. For explanation, see the link provided by @JackFlamp and @Kent – deHaar Aug 22 '18 at 12:33
  • My confusion comes from here, if interfaces do not inherit from object... reading the post of Precise definition of "functional interface" in Java 8 where says "A functional interface may specify any public method defined by Object, such as equals( ), without affecting its “functional interface” status. The public Object methods are considered implicit members of a functional interface because they are automatically implemented by an instance of a functional interface" – silvia Dominguez Aug 22 '18 at 12:38
  • 1
    @silviaDominguez Yes, that means that `toString`, `equals`, `clone` etc get implemented anyway so they don't count when creating a functional interface. It's probably only a compile time issue. Not a runtime issue.. – Jack Flamp Aug 22 '18 at 12:43
  • Ok, thanks so much. That clarifies it to me a lot more!! – silvia Dominguez Aug 22 '18 at 12:50

1 Answers1

5

According to Java API,

An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method. Since default methods have an implementation, they are not abstract. If an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.

Panduka Nandara
  • 504
  • 6
  • 14
  • 1
    Correct, but your link is a link to the Java API, not to the Java Language Specification. [This](https://docs.oracle.com/javase/specs/jls/se10/html/jls-9.html#jls-9.8) is the relevant part of the Java Language Specification. – VGR Aug 22 '18 at 14:12
  • Oh sorry I have edited that. – Panduka Nandara Aug 22 '18 at 14:33