Win a copy of AWS Security this week in the Cloud/Virtualization 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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: 69527
277
  • 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 :-)
 
Bartender
Posts: 3959
155
  • 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: 69527
277
  • 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: 84
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: 12030
257
  • 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.
 
Poop goes in a willow feeder. Wipe with this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic