Translate

Search This Blog

Wednesday, 13 June 2018

Java8 - Functional interfaces

Functional interfaces
Java8 has added to it's kitty another concept know as functional interfaces.

So what exactly is an functional interfaces?

  • Functional interfaces are interfaces with exactly a single abstract method (SAM). 
  • It is represented by the annotation type @FunctionalInterface. 
    • This annotation let's the compiler know that the interfaces should adhere to the contract of SAM.

NOTE:

  • A couple of things here that this interface can also have other default functions but not another abstract method. 
  • These interfaces can also be extended by another interfaces and can be called functional interface as long it does not have another abstract method.
  • Also compiler will treat all SAM interfaces as functional regardless if the annotation is present or not.


So how do we use this interfaces?
These interfaces can be represented using

  • Lamda expressions 
  • Method reference and 
  • Constructor reference.(Lamda expressions another important concept I have covered in another blog article)


Examples of functional interfaces
in Java8 are Runnable, Comparator, ActionListener, Callable etc.



No comments:

Post a Comment

Please use proper blogging etiquette while posting comments.

Note: only a member of this blog may post a comment.