About Ramesh Kotha

Ramesh is a certified Java professional working in financial domain, He is an application developer and has hands on practice with Java and related technologies. He is passionate about Spring and other open source technologies.

Java 8 : Functional Interface Example

To Support lambda expressions in Java 8, they introduced Functional Interfaces.

An interface which has Single Abstract Method can be called as Functional Interface.

Runnable, Comparator,Cloneable are some of the examples for Functional Interface. We can implement these Functional Interfaces by using Lambda expression.

For example:
 

Thread t =new Thread(new Runnable(){
   public void run(){
     System.out.println("Runnable implemented by using Lambda Expression");
   }
});

This is the old way of creating a Thread.

As Runnable is having Single Abstract Method, we can consider this as a Functional Interface and we can use Lambda expression like below.

Thread t = new Thread(()->{
   System.out.println("Runnable implemented by using Lambda Expression");
});

Here instead of passing Runnable object we just passed lambda expression.

Declaring our own Functional Interfaces:

We can declare our own Functional Interface by defining Single Abstract Method in interface.

public interface FunctionalInterfaceTest{
void display();
}
//Test class to implement above interface
public class FunctionInterfaceTestImpl {
      public static void main(String[] args){
     //Old way using anonymous inner class
     FunctionalInterfaceTest fit = new FunctionalInterfaceTest(){
        public void display(){
           System.out.println("Display from old way");
        }};
     fit.display();//outputs: Display from old way
     //Using lambda expression
     FunctionalInterfaceTest newWay = () -> {System.out.println("Display from new Lambda Expression");}
        newWay.display();//outputs : Display from new Lambda Expression
     }
}

We can annotate with @FunctionalInterface annotation, to tell compile time errors. It is optional

for ex:

@FunctionalInterface
public interface FunctionalInterfaceTest{
   void display();
   void anotherDisplay();//shows an error, FunctionalInterface should have only one abstarct method.
}

Default Method:

Functional interface can not have more than one abstract method but it can have more than one default methods.

Default methods are introduced in Java 8, to add new methods to interface with out disturbing the implemented classes.

interface DefaultInterfaceTest{
  void show();
  default void display(){
     System.out.println("Default method from interface can have body..!");
  }
}
public class DefaultInterfaceTestImpl implements DefaultInterfaceTest{
   public void show(){
         System.out.println("show method");
   }
   //we dont need to provide any implementation to default method.
   public static void main(String[] args){
          DefaultInterfaceTest obj = new DefaultInterfaceTestImpl();
          obj.show();//out puts: show method
          obj.display();//outputs : Default method from interface can have body..!
        }
}

Main use of default method is with out forcing the implemented class , we can add a method to an interface.

Multiple Inheritance:

If the same default method is there in two interfaces and one class is implementing that interface, then it will throw an error.

//Normal interface with show method

interface Test{

  default void show(){
     System.out.println("show from Test");
  }

}

//Another interface with same show method

interface AnotherTest{

   default void show(){
      System.out.println("show from Test");
   }

}

//Main class to implement above two interfaces

class Main implements Test, AnotherTest{
//here is an ambiguity which show method has to inherit here
}

This class wont compile because there is an ambiguity between Test, AnotherTest interfaces show() method, to resolve this we need to override show() method to Main Class.

class Main implements Test, AnotherTest{

   void show(){
      System.out.println("Main show method");
   }

}
Reference: Java 8 : Functional Interface Example from our JCG partner Ramesh Kotha at the java2practice blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

2 Responses to "Java 8 : Functional Interface Example"

  1. ali says:

    nice article. where is comments / all comments feed ?

Leave a Reply


5 + = nine



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close