Skip to main content

Functional Interfaces using Lambda Expressions


Functional Interface:
Interface with a single abstract method.
→Can have more than one default method.

You can check my post about the difference between abstract class and an interface here:
https://sairohithkaranam.blogspot.com/2020/04/what-is-abstract-class.html

→The implementation of the abstract method of the functional interface can be done in two ways
     →Traditional way
     →Lambda Expressions
→In this post, we will see both the ways.

Functional interface via Traditional way:
@FunctionalInterface //Optional
interface A
{
    public void m();//abstract by default, one and only abstract method must be there
    default void m1()//default method
    {
        System.out.println("The Developing Minds");
     }
}
class Demo implements A
{
     public void m()
     {
         System.out.println("Hello people!");
     }
    public static void main(String...args)
    {
         Demo d=new Demo();
         d.m();
         d.m1();
     }
}
Output:
 Hello people!
 The Developing inds

Functional interface via Lambda Expressions:
@FunctionalInterface //Optional
interface A
{
    public void m();//abstract by default, one and only abstract method must be there
    default void m1()//default method
    {
        System.out.println("The Developing Minds");
     }
}
class Demo
{
   public static void main(String...args)
   {
          A a=()→System.out.println("Hello people!");//Lambda Expression!
          a.m();
          a.m1();
    }
}
Output:
Hello people!
The Developing Minds

Now after seeing these two codes, you've understood what is the basic syntax of lambda expressions and how it is used.

Remember!

→()
    These brackets in lambda expression indicate the parameter of the method.
→After the arrow(→) of the lambda expression, that was your actual implementation of the abstract method.
→So, the lambda expression, "()→System.out.println("Hello people!")" indicates that a method with no parameters is implementing a print statement. Cool, isn't it?

Though both methods give the same output, we prefer to choose lambda expressions often. Why?

Advantages of using Lambda Expressions over Traditional way:
Faster
→Multiple implementations of a single method in Functional interface.
    →In the above example, we can also write another implementation of the method 'm'.
         class Demo
         {
            public static void main(String...args)
            {
                 A a=()→System.out.println("Hello people");//Lambda Expression
                 //This line can also be shortened like this: A      a=System.out.println("Hello people!");
                 A a1=()→System.out.println("Hello world");//Second implementation of same method
                 a.m();
                 a1.m();
             }
          }
           Output:
            Hello people!
            Hello world

      crux points:
  1. A functional interface is nothing but an interface with a single abstract method.
  2. We often use Lambda expressions for the implementation of abstract methods in the functional interface because it is faster.
  3. There are many inbuilt functional interfaces in java like Runnable, Comparable, ActionListener.
Practice and work more about lambda expressions, then you can just play with them in your code!!


Comments

Popular posts from this blog

First post!

Hello World, My name is Sai Rohith Karanam, currently pursuing BTech Computer Science Engineering at Lovely Professional University, Punjab. I am a tyro in writing blogs. I'm just exploring a new field in my life. Actually, I would like to share something that happened before writing this blog. From the past 5 days, I was constantly trying to write a blog and was searching like hell all over the internet. All the questions in my mind like, where to publish my blog? what to write in my blog? These type of questions kept flashing through my mind honestly till today afternoon. I then saw some videos of "how to own a domain name and buy web hosting for a cheaper price", the cheaper price I finally got is 6000 INR, but I don't want to take a risk by affording money as I am still a novice. If at all I want to afford the money, let me gain a little bit of experience by writing here and then afford it later. Then, all of a sudden, an idea flashed through my mind and s...

10 lessons I have learnt in 2020...

2020 is a year of shocks, surprises, learnings and a lot of introspection for me. I know with the arise of corona pandemic, many people have lost their lives, lost their jobs, lost their own families but still, there are a lot of things we need to be grateful for. For example, at this moment, you are alive, you are reading this, your senses are working properly and above all, your health is perfectly alright in spite of the pandemic.  Now tell me, how many good things are there that we need to be grateful for amidst of corona chaos? Every year is an experience we gain for the future, experience from the mistakes that we've done, experience from the way we have responded in a particular situation and experience from our self-fulfilling prophecies. So like everyone else, this year has really taught me some valuable lessons which I believe are the first step to achieving my goals. So I thought, why not share my experience of this year with people so that they may learn something out o...

Purpose in Life vs Purpose of Life

A gentle disclaimer, This post is only written on the basis of my thoughts and perspectives. It is not intended to hurt anyone's beliefs and traditions. Okay, let's start. We study at prestigious universities, we get the highest paying jobs, we get lots of money which is well enough to buy every single thing for our own comfort, our parents will be very proud of everything we have achieved and a lot more praise from the society. Are these enough for your life to be successful? If you said no, you're on the right path. But if you said yes, Think again! Today we are in the year 2020, let's move a 1000 years back. Because you were born in the 20th or 21st century, you are struggling to become a doctor or an IT professional. What if you were born 1000 years ago? Well, you might opt fishing as your career, right? What did this symbolise?  Our purpose in life, which means it is the purpose which we should fulfil to make a living for our life keeps on varying.  Today, you are ...