• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Doubt on functional interface

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is a functional interface ?

1. It's an interface that always contains exactly one abstract method. It may contain default/static methods.
2. It's an interface that contains zero or one abstract method. It may contain default/static methods.
3. None of these.

Which one of the two statements are right? Thanks in advance!
 
Marshal
Posts: 64654
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which do you think is correct?
 
Neha Agnihotri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your message, Sir.
AFAIK, functional interface is basically used in evaluating lambda expressions. So, at least one (and only one) abstract method should be present in an interface to qualify it to be called as a functional interface. Going by this logic, I feel option 1 is correct. Please correct me if I am wrong. TIA :-)
 
Saloon Keeper
Posts: 3293
146
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Neha,

I would also go for option 1. But there was a topic the other day about why Comparator was a Functional Interface, since it has two abstract methods. I advise to give it a read:

Comparator interface
 
Neha Agnihotri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This was very informative.  Many thanks for your reply, Sir.
 
Campbell Ritchie
Marshal
Posts: 64654
225
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option 1 with exactly one abstract method is correct. The definition of a functional interface is to be found in the Java┬« Language Specification (=JLS).
 
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neha Agnihotri wrote:Which is a functional interface ?

1. It's an interface that always contains exactly one abstract method. It may contain default/static methods.
2. It's an interface that contains zero or one abstract method. It may contain default/static methods.
3. None of these.

Which one of the two statements are right? Thanks in advance!

I go with 1 and 2
Because there is a chance of one abstract method in 2.
 
Saloon Keeper
Posts: 10308
217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be wrong.

A functional interface must have at least one abstract method, and may not have more than one abstract method that doesn't share a signature and return type with a method from the Object class. However, since functional interfaces usually don't redeclare methods from the Object class, it's easier to say "Functional interfaces must have exactly one abstract method declaration", even though it's technically incorrect.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!