Functional Interface in Java 8

UPDATED: 12 June 2015
Functional Interface Java 8

In my previous article I explained Difference between Abstract class and Interface in Java. It gives you insight on interface in Java. And Now...

What is @FunctionInterface?
In Simple Term...
Any Interface annotated with @FunctionalInterface, instructs compiler to check that, this Interface should have only one abstract method declared in it.

Characteristics of Functional Interface.
- You can have only one abstract method in Functional Interface.
- You can have default methods and static methods in Functional Interface because they are not abstract.
- You can have public methods of java.lang.Object class in Functional Interface because these method won't count towards Functional Interface's abstract method.

Source Code
/**
 * Any media that is <code>Playable</code> in mobile should implement this interface.
 */
@FunctionalInterface
public interface Playable {

    public void play(String file);

    /* public method of java.lang.Object class */
    @Override
    public boolean equals(Object obj);

    /* public method of java.lang.Object class */
    @Override
    public String toString();

    /* static method to get default player */
    static String defaultPlayer() {
        return "Google Play";
    }

    /* default method to identify file type */    
    default String fileType(String strFilename) {
        if (strFilename == null || strFilename.isEmpty()) {
            return "Provide valid filename";
        }

        if (strFilename.endsWith(".mp3")) {
            return "Audio File";
        } else if (strFilename.endsWith(".mp4")) {
            return "Video File";
        } else {
            return "File not supported";
        }
    }
}

Source Code(Using in Lambda Expression)
public class Motorola {

   public static void main(String[] args) {
      /* Implementing Playable interface using Lambda Expression */
      Playable playable = (String file) -> { System.out.println("Playing Music: " + file); };

      /* Call play */
      playable.play("Pitbull - Give Me Everything ft. Ne-Yo, Afrojack, Nayer.mp4");

      /* Using Interface as parameter in method */
      MusicPlayer((file) -> { System.out.println("Playing Music: " + file); }, "Pitbull - International Love ft Chris Brown.mp4");
   }

   /* Method using Playable interface to play music file */
   public static void MusicPlayer(Playable playable, String file) {
      playable.play(file);
   }
}

Output
Playing Music: Pitbull - Give Me Everything ft. Ne-Yo, Afrojack, Nayer.mp4
Playing Music: Pitbull - International Love ft Chris Brown.mp4

Myth about @FunctionInterface and Lambda Expression
Many developers think if we want to use any Interface in Lambda Expression then it must be Functional Interface. Its completely wrong!

You can use any Interface in Lambda expression that has only one abstract method in it, even if you not annotated Interface with @FunctionalInterface.

But if you are planning to use your Interface as Lambda Expression then annotated your Interface with @FunctionalInterface because compiler will check that this Interface should have only one abstract method at compile time. And also when other developer see that this is functional interface so they won't add new abstract method in it.

Java 8
default and static method in Java Interface

0 comments :