• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparator interface in java is a functional interface... why?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The functional interface in java are those interfaces which has only one method to implement. Comparator interface in java 8 has 2 non default methods ie compare(T o1, T o2) and equals(Object obj). It is still called functional interface. Why?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals() method actually comes from class Object. All objects in Java already have an implementation of the equals() method, because they inherit it from class Object.

So, Comparator is a functional interface because there's only one unimplemented abstract method: compare(T o1, T o2).
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a definition of a functional interface in the Java® Language Specification (=JLS); it says specifically that methods inherited from Object do not count towards the definition of a functional interface.
Default methods are also excluded which means the Java8 version of Comparator has 18 methods and is still marked @FunctionalInterfaceEasier explanation to understand than in the JLS. It has one method from Object and 16 default methods, leaving only one fully abstract method.
 
Ajit Kr Sharma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks @campbell, @Jesper for the detailed explanation.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Our pleasure
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic