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.
Biniman Idugboe wrote:In the above anonymous class, the equals() method was not overridden.
Besides, the right hand side of the lambda expression evaluates to an instance of a BiFunction<Apple, Apple, Integer>. How does a Bifunction become a Comparator?
Biniman Idugboe wrote:Can we implement an interface that has multiple abstract methods with a lambda expression?
Are we allowed to provide a body for one of the multiple abstract methods and forget about the other abstract methods?
Lambda expressions create an object that inherits from the Object class, so it also inherits the implementations of the methods declared in Object.
Functional interfaces are allowed to redeclare the methods that were already declared in Object and the effect will be the same as if they weren't declared at all.
You'll notice when you read the JavaDoc that Comparator.equals() has a more specific description than Object.equals().
Biniman Idugboe wrote:I have not read the documentation in much detail, but I do not know whether the Comparator interface extends the Object class.
Is it the case that the equals( ) method in Object class takes precedence over the equals( ) method is Comparator interface?
If that is so, why does the equals( ) method in Comparator not count when implementing the Comparator as in the above?
Is the equals( ) method in Comparator useless?
This method must obey the general contract of Object.equals(Object). Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that sgn(comp1.compare(o1, o2))==sgn(comp2.compare(o1, o2)) for every object reference o1 and o2.
If an interface provides a default implementation for a method, implementations in classes always take precedence. That's why in an interface, you can't give default implementations for methods that are also declared in Object: their implementation would always be overridden by the implementation given by Object, and therefore useless.