0

How can Predicate be considered and used as a functional interface when it has several abstract methods? For example: As well as test(T t); it has: and(Predicate<? super T> other), negate() and isEqual(Object targetRef ).

This question is not a duplicate: I am already aware of the answers given for comparator, but they do not apply here because negate and and are not inherited from the Object class or any super class.

IqbalHamid
  • 2,324
  • 1
  • 18
  • 24
  • 3
    Predicate has _one_ abstract method and that is `test`. nothing else is abstract in that interface. – Ousmane D. May 22 '18 at 19:12
  • @Aomine I just explained this question is NOT a duplicate. I have seen the answers here: https://stackoverflow.com/questions/43616649/how-can-comparator-be-a-functional-interface-when-it-has-two-abstract-methods and here: https://stackoverflow.com/questions/23342499/why-functional-interfaces-in-java-8-have-one-abstract-method – IqbalHamid May 22 '18 at 19:12
  • 5
    Those methods are not abstract, they all have bodies. `and` and `negate` are `default` methods, and `isEqual` is `static`. – Jorn Vernee May 22 '18 at 19:12
  • @Jorn Vernee I considered that may be the case but am confused as the Oracle documentation for Predicate appears to offer and and negate as alternative methods to test. isEqual() comes from Object class so I understand why that method can exist. – IqbalHamid May 22 '18 at 19:15
  • 1
    @IqbalHamid [`Object`](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html) does not have a `isEqual()` method. – Ousmane D. May 22 '18 at 19:24
  • You might want to read up on [default methods](https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html). – shmosel May 22 '18 at 19:29
  • OK studying the Oracle documentation more carefully, I can see that test(T t) is the only abstract method as the others are default as stated by Jorn Vernee. – IqbalHamid May 22 '18 at 19:30
  • Thank you shmosel, I think you are right. It is gradually making more sense. – IqbalHamid May 22 '18 at 19:31

0 Answers0