Functional Interface | Lambda Expression in Java

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] and in this video we'll talk about Lambda expression in Java the thing is Java 19 has already released Java 20 will be coming next march and now we are talking about a feature which was launched in 2014 which was in Java 8. now the question is why we are talking about Java 8 feature now because it is one of the most important and most amazing feature introduced in Java after I could say generics and collection API so in 1.2 we got collection API in 1.5 we got generics and now in 1.8 we got Lambda expression now even before we talk about Lambda expression let's talk about interfaces now what happens is whenever you design an application you design the application for different classes right see ultimately it's all about objects and to create the object first you write classes right and then sometimes to to make it more abstract to define the behavior we go with the interfaces so basically we declare the behavior in the interface and we Define the behavior in the class right again data design decisions you take what interface to create what classes to create but basically we have a concept of interfaces and we have talked about it before the idea here is when we talk about interfaces a interface can be of different types now depend upon how many methods you have example in one interface you can have multiple abstract methods right now in that case that interface becomes a normal interface a interface can have one method now in Java 8 it is called a functional interface now before Java 8 it used to call Sam which is single abstract method and then we got a special interface which doesn't have any method I know that sounds weird which is called a marker interface but in this video let's not talk about micro interface let's talk about the functional interface that's right now you might be thinking why someone would use an interface which has only one method what if I say most of the interfaces in Java are functional interface and in Java 8 it cost a special feature okay I will show you that here so let's talk about function interface as I mentioned before a functional interface is an interface which has only one method okay so what I will do is I got my IntelliJ IDEA already here and in this SRC let me create a Java class we'll name this Java class as demo because I just want to write everything in one file so this is my demo file and in this I want a main method so I can say main control space done so that's my main method but will not be using this at this point I want to create some classes interface right this is what you want this is where you want to see the output so what I want to do is I want to create a simple interface here as I mentioned before it's all about interfaces for this video so I will create an interface and we'll name this interface as a again not a good way of naming your interfaces but just for the example I'm creating an interface called a and in this interface I can mention multiple methods right and Abstract methods now basically in Java 8 things have changed a lot of people were against this concept where you can Define the methods and interface so this those are called default methods but let's not talk about those things let's only talk about abstract methods so here basically we can have multiple methods right but we want to make sure that this is a functional interface now if you want to restrict an interface to have only one method example let's say if I go back to this interface and if I say void show let's say I want to have a show method here and apart from this I also want to have a method which will return a int value called add which may take some parameters doesn't matter so you can see I have a interface which has two methods in fact you know let me just increase the font size a bit okay so what I will do is here I will just make this as a function interface now this is a special annotation given to the interface where you can only have one method so if you have two methods and if you make it functional interface it will not work you can say it gives you an error it says multiple non-overriding abstract methods found in interface a okay that simply means in English terms is you cannot have more than one method so we can remove this now that becomes your functional interface as simple as that okay and now I can create a class so basically can I create an object of interface let's try that and everyone knows right we can't do that and this is one of the most famous uh interview question can I create object of interface and the answer is no you can't create object of interface but yes you can create an implementation and then you can create the object so can I say we can create the object of the implementation of the interface okay what I'm simply saying is we can create a class which implements the interface and then we can create the object of a class okay so if I come back here if I say a obj equal to new a of course this will not work uh you can see it will give you an error if I go back here it says a is abstract cannot be instantiated okay makes sense so in this particular video we'll also talk about Anonymous class okay Anonymous in a class uh to understand this more so what I will do here is I have the interface and then I'm trying to create the object which will not work one of the way you can do that is by creating a class I can say Class B and we have to say implements a of course this is one way and in this particular class I can create a method basically I can Define this method which is show so I can say public void show and basically I don't want to do something special I can simply type hi that sounds weird but let's go with high here and now instead of creating object of a we can get object of B this is what I will I have mentioned before right uh okay there's no error and now using this obj again called show nothing fancy till this point right simple stuff we just created the object and now if I want to run this code I can right click and say run demo and we got the output you can see that we got high so this is working but then the idea is I don't want to create a submit class now first of all why will you get a submit class uh it's because we wanted to implement the interface and to do that we create a submit class but what if this class will be used only once in the entire project you're going to create only one object of this class then why to create a submit class do we have a solution well I don't want to get a class now you might be thinking what's wrong with getting a class the only thing is if you have more classes you have to maintain more files you have to maintain a lot of documentation for one file so it's better to avoid classes when required if you know that it will be used only once you can avoid it but how we'll talk about it in some time but there's one more thing remember when I started this video I mentioned that in in the functional interface we can have only one method there's only one twist here example let's say of course if I create a method which is which which says something like this if I say ain't add it will give you some bad words you can see it says uh you cannot have more than one non-awiding methods now what it means it simply means you cannot have more than one method right but then what is not overriding here let me show you something if I say string to string and bracket you can see there is no error now this is not complaining function interface says hey that's fine two string works you know why this works is because two string is a method which is there in the object class and every class in Java extends an object class example if I say extends object and if I click on this object you can see object class has lot of methods and one of them one of the method is tostring if I scroll down down yeah can see that we got two string method now what happens is every class in Java extends object class and that's why the two string method is already a part of a class and that's why function interface says that's fine if you use any method which is there in the object class it is allowed because any weight is getting implemented so just it will not complain okay so basically it has only focus on show method and that's why it says it's on it has only one non-over-ready abstract method okay uh time in that symbol let me remove this I just wanted to show you that concept there but now I got this right I don't want this class B so the other way of doing this I will just comment this part since we don't want it I can just comment it and now how will I work with B so what I will do is I will go back to my a and here but I will create the object see you can't create the object of a is because we don't have the implementation but what if we can create the implementation there itself that's right so just after your new a round package we can write the implementation a class itself okay and if you can do this this particular concept is called Anonymous inner class because we are creating a class without a name you can see we don't have B anymore earlier we had a name for the class this time we don't have a name second it is inner class why this inner class is because we are creating a class inside a class demo so that's an inner class so we can say this is Anonymous in a class and in this Anonymous in a class basically you can Define your method so which method we are talking about this show method so I can come back here and I can say public void show and I can basically print the same thing I can print hi okay or maybe I can say hi in show just for different text and okay that's it let's run this quote and let's see if this works so basically what we are doing is we instead of using a normal plus with a name we are going for anonymous inner class I will right click and it will say one demo and that worked and so that we got high in show this works but now we have a Twist The Twist is sit this was a syntax which we were using till Java 7. and in Java 8 they introduced something which is Lambda expression so what Lambda expression says is now think about this see as a human we know how to complete the sentence right example from our childhood we are learning something like Johnny Johnny so someone will say yes papa right so basically we try to complete stuff in the same way Java says hey it's a obj now a is the interface and you're trying to say obj equal to of course someone will say new a right that's if you can complete that why not Java why not your compiler says hey you don't worry you simply say aobj I know how to complete the sentence I know how to say new a because we know the interface name I also know how to open the curly brackets I also know how to write public Voice show okay but how exactly your compiler will know which method you want to Define it's because a is a functional interface which has only one method so of course your compiler can also write this part public white show and then as a programmer your job is to only Define what goes inside the method so don't do things as a programmer you should only focus on the logic not these two lines that's what your compiler says you can remove it okay but then you can see we got the error but remember this thing when I remove when I'm removing that part I'm also removing this curly brackets here so if I remove this curly bracket I have to also remove this outer curly packets here the ending one so what I will do is I will just delete this part and I also remove the curly brackets from here I will put the semicolon on the same line okay but still there's the error now we have to Define that this particular block belongs to a method which is your show method so in that case we can just put a arrow here now this arrow is also called Arrow function or a Lambda expression right now people who are coming from JavaScript it's very similar we use double equal to and the angular brackets here we use Lambda expression this is called Lambda expression here or we can say Arrow but then let's be Java term which is Lambda here we did the same thing which we have done here only this number of lines now the beauty is this is a block this is a block which defines the method this is the parameter this is the parameter for the method example in show if you can pass some parameters you can do it here as well so whatever down packets we have here is same as this so if you pass some parameters you can mention that here and you can also mention that here this is a Lambda expression and this block can have multi multiple statement okay that perfectly works but what if you only have one statement in that case you can simply remove these curly brackets and since we have two semicolon I can remove one and we can write everything in one line I know you you may need to watch this video multiple times because I've done I've deleted some code and you have to try it on your machine first otherwise it's very difficult to understand so make sure that you try this out and this is how your lambdaction looks like Okay so this code from here to here is a replacement for this entire code okay so this is a Lambda expression the one thing to remember this thing will not be called automatically you need to call it okay you are defining the function here but you have to call the function as well and now let's see if this works I will say right click one and it worked can you see that we got high in show so that's how basically you use Lambda expression in Java of course it has multiple other features as well what if you want to return a value what if you want to pass the values that will see in the upcoming videos
Info
Channel: Telusko
Views: 39,626
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: 4HC_WyBSDGA
Channel Id: undefined
Length: 13min 56sec (836 seconds)
Published: Tue Oct 11 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.