Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Doubt on functional interface

 
Ranch Hand
Posts: 39
  • 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: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which do you think is correct?
 
Neha Agnihotri
Ranch Hand
Posts: 39
  • 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: 3413
149
  • 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
Ranch Hand
Posts: 39
  • 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: 65062
247
  • 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: 58
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: 10420
223
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!