hey guys welcome to concept encoding and this is shransh so in the interface uh topic this is the last topic functional interface and Lambda expression this is a Java 8 feature and asked a lot in the interview right so this is very very important just a heads up okay so now let's start with this let's see what we are going to cover today what is functional interface what is Lambda expression how to use functional interface with Lambda expression advantages type of functional interface and this is one of the use case because you know that interface
can do a nesting from another inter not nesting the functional interface can extend from another interface so what are the different use cases can come okay so now let's start with what is functional interface so if an interface contains only one abstract method then it is known as functional interface you know that whenever we create an interface right and if we don't Define anything by default what it is it is public and abstract right even if you don't write into an interface then you know that public and Abstract will be added automatically right so this
is if you can provide only the signature this is known as abstract method this is already we are clear so if an interface contain only one abstract method only one abstract method that is known as functional interface okay it is also known as Sam interface Sam is single abstract method the same thing single abstract method okay so at the rate functional interface keyword can be used but it is optional so here if you see that I have created an interface bird and created one abstract method I haven't written abstract you know that in an interface
it will be added public abstract automatically so this is an abstract method and I have provided only the signature right so this is an abstract so I have used the functional interface keyword but this is also equivalent to this also you can you don't have to use at the rate functional interface this is just an optional so this will also be known as functional interface because why it has only one abstract method okay but what is the difference like if you put functional interface and if you try to add another so now here if you
see I have used the functional interface keyword now if you try to add another abstract method you will get a compilation error see okay so functional interface annotation or keyword Force you restrict you that you cannot add another abstract method but if you don't use add the red functional keyword if you have only one abstract method it would be known as functional interface but it will not force you if you want to add another you can add it get height then it will not be a functional interface then because now it has two abstract methods
okay so you know right why a functional interface keyword you can use when you have to restrict that no other abstract method can be added okay just see this also this example so I have put an at the rate functional interface keyword and created one interface board and this is my one abstract method this is my one abstract method now see this what definition says that functional interface contains only one abstract method means you can have other methods like default so I can have a default method which has the implementation so only one abstract method
is there I can have the default method I can have a static method which is and even the object class method so you I have already told you in the class topic that each class right is the child of object class so in object class has certain method like uh two string equal I think hash code so these are the methods which are present into object class so these methods are not considered as an abstract here if you see that see this example normal not functional interface just wanted to know show a normal let's say
I have created an interface test interface and I have defined a method two string right this is similar to uh what we have into an object class right two string also the same is present the same it would be by default public so public string two string method definition is present here now if you create a class which implements this interface now you see that you don't have to provide its definition you don't have to provide a definition why because you know that each class by default extends from the object class itself okay so generally
that's why we say that so in the interface if you define any object class methods you don't have to provide the implementation for it it's not required okay so this was just a normal interface here if you see that I have created a class which is extended this interface and see no error I haven't even implemented this why because this class by default extend this object class and definition is already provided here okay so in the functional interface you can have only one abstract method but you can have default method you can have static method
or you can even have the methods which are present into an object class okay so these are all allowed so this is still a function interface because it has only one abstract method even though it has other methods like default static or objects so in functional interface only one abstract method is allowed but we can have other methods like default static or methods inherited from the object class okay so this is clear so this is what known as functional interface okay so this is clear right what is functional interface an interface which has only one
abstract method but you can have default static and other methods also but only one abstract should be there now the question comes is hey then what is Lambda expression and why it always comes whenever we discuss about functional interface how they are both related to each other this thing should come to a mind right let me explain you with this uh just don't see this now let me first explain this this so what are the different ways to implement the functional interface or you can say the normal interface right so first way as we already
know using implements keyword so now let's say I have created a functional interface it's a very basic interface word and has one abstract method called can fly so you know that you we can create one class let's say for the bird I have created one class Eagle I have implemented this interface using the Implement keyword right so now it has to provide the definition of this can fly so it provided the definition of this so this is one of the way so now I can create an object of eagle right so new Eagle I have
created an object of eagle and the object references is stored by the the bird interface but this is only just a reference and you can call this method this is we have already seen right this is very basic so this is one of the way you can implement the functional interface like whatever the method you have use the Implement and do it what is the second way so this was the first way what is the second way second way is using Anonymous class if you have watched the on class class topic video I have explained
what is anonymous class right a class without a name it's called Anonymous class so let's say this is the interface functional interface I have a interface but same one abstract method right and in the main method without T1 creating class Implement and all I can create an anonymous this thing so bird an object right it will hold the reference of the object which you will create now how you will create an anonymous class so new bird and provide the implementation here right so provide the implementation here override this method can fly and provide this the
complete implementation so new bird and then the block starts block end here and semicolon and within this you have whatever the method you have you have to provide its definition right and so this Eagle object which holds the reference of the whatever the new internal so internally it will create a class implemented but it's an anonymous in the example I showed you that it will create some with a random name which compiler decides right the name so it's in that's why it's known as Anonymous so internally what it's doing is nothing but the same it
will create one class with its own name and implement this bird then write a method this implement this method and using this Eagle object you can access this can fly vertical or not okay so this is we have already seen that how you can implement the anonymous this is known as Anonymous class right so we have already covered that one so if you are totally not recalling it so I will suggest that please check what is anonymous class because there we have explained in depth like internally what it does it create a new class itself
with the name which is decided by the compiler but this is the way through anonymous now what is the third way so Third Way is using Lambda expression so now you know that while Lambda expression is always comes into the picture when we always talk about the functional interface so it's some way of implementing the interface Lambda expression is a way to implement the functional interface one way is using Implement second way is using Anonymous Third Way is now Lambda in Java 8. right now you say that hey like why we need Lambda expression to
implement when we have already two ways to implement interface so note is one thing in this two so let's say this Anonymous class so here you have to implement this method an interface which has only one abstract method only one right so here if you see there are too much verbose so only this thing this line is your which is you required rest is all Java verbose like create a new this block start put override put a method can fly and then do this so here if you see that in the with the help of
Lambda expression the main intention here is to reduce this verbose right so that's why they have used with the functional interface because in functional interface we know that there would be only one only one abstract method so why we need to provide this method name right because we know that functional interface will have only one abstract method so there is no need to provide the like which method you want to overwrite it's know that it would be only for the one abstract method so it tried to reduce the verbose so now let's see how the
Lambda expression can be used so the same thing I have a functional interface basic and the can fly one abstract method then the main method so instead of anonymous now I am using a Lambda expression see how so bird and this will hold the reference of the object okay now here if you see that this is what Lambda expression is so this is you can say that generally the arrow operator or generally the Lambda operator generally we can say Arrow operator or Lambda operator so first is this okay if your abstract method does not accept
any input parameter then it would be empty if it accept any input parameter then you can provide that input parameter you don't need to give the name and all that is not even required because Lambda expression is you have only one method one abstract method in a functional interface so no need to use the name can fly and all so if your method doesn't accept any input parameter it would be empty if it accept provide it over here so can fly except one input parameter that is string well so I have given one string and
it can be one what is the name of the value then this is the arrow operator which says that okay okay we are doing a Lambda implementing the functional interface right so this is the Lambda operator and then you can start the block finish the block semicolon at the end and you can just provide the implementation what we were giving here okay if you have more than one one line right if you have only one line then this block also not required it would simply work like this uh system dot out Dot println right system.org
dot println that's all finish if you have more than one line you have to provide the block so now you here see that like lot of verbose has been removed out of this using this Lambda expression okay so first thing is start this block provide the arrow operator and if it is only one line you don't need this blocks if you have more than one line then you need this blocks you need to provide this block and inside this provide the implementation and we can similarly access this like Eagle object dot can fly so you
got it what is the use of Lambda expression right why it's always used with function interface okay so just don't worry about this Lambda expression like it will get more clear with the different examples which I am going to give you so now let's see the type of functional interface there are four types right which is already inbuilt you can say that this is already inbuilt right sometimes you don't have to write function interface you can use it what is already existing so first type is consumer so if you see this package Java dot util
dot function inside this package you will see this all four consumer supplier function and predicate okay so let's see one by one what is consumer functional interface so here what would happen is represent an operation that accept a single input parameter and return no result so its functional interface looks like this it has ADD the red functional interface then an interface with name consumer right with the name consumer and it's a generic type so I think you already know we have already covered gender classes right you already know uh how this has been handled right
so this is a generic interface right but it's an functional it means only one abstract would be there one abstract method would be there so what is the name of the abstract method here except okay so what this consumer says that this functional interface whatever the abstract method it has it will accept only one except a single input parameter only single input parameter and returns no result returns no result okay let's see supplier what supplier says that represent the supplier of the result except no input parameter but produces a result so very similar to this
one I have created a functional interface supplier it is already there in this package okay again it's a generic one means you can return anything string Boolean integer whatever you want right so here if you see that what this definition says that accept no input parameter so no input parameter and produces a result so it produces the return okay now let's see one by one I think you got the just like what is the types of functional interface like so consumer we already know that uh this functional interface already exists and with x sub 1
input parameter but returns no output okay and how you can use it so let's say I have created one main class main method right and I have creating a variable reference where object reference where I will store the object whatever it's gonna create right and in this one I want to let's say pass integer so I am passing an integer one right so it except one parameter right so I it's a generic right so either you can pass integer uh string Boolean whatever you wanted so I am implementing it with an integer so it accept
one input parameter so this is not empty I am passing one integer variable here okay then Arrow that means it's a Lambda expression and we are implementing the functional interface and I have more than one line so I am using the block block or start block and semicolon and inside this you can do anything you don't have to return anything you just process it so I'm getting a value so I am just processing it hey value is greater than 10 I'm just putting a print logging do something okay so some processing and whatever the objects
reference it whole logging object you can call this accept method right and you pass the value 11. so it will internally invoke this one it will pass 11 here 11 greater than 10 it will print login right very very similar to Anonymous but only thing is certain verbose has been reduced similarly when to use supplier now if you you can use supplier when you don't want to pass any input parameter but you want to get certain out of it okay so now let's see here I am passing so this is the functional interface now I
am implementing supplier this is my object reference and this is the generic so I have to tell like what type of weather string integer or any I have to give the type okay so here I have given the type now here is it accepting anything no so that's why I have created an empty one and I don't want to create a block I have only one line so here if you see that I haven't started or ended the block I just simply this is the data I am returning so it will simply return it okay
and this is very equivalent to this what we have seen till now supplier what is the generic type string this is the object reference no input so it is just like this Arrow parameter Lambda parameter you can even start the block and the block even for one line also but now you can like this return this is the data I am returning right so this is the two way for one line you don't need a block and this don't have to write a return it will simply return but when you are creating a block it
would be like a normal you have to return this like this way right and how you can call whatever this object reference you have created dot get so this is supplier no input but there is the return similarly there is a function function says that accept one argument parameter and produces a result so means it accepts also it returns also okay so the function interfaces looks like this it's a functional interface only one abstract method and then the generic type it accepts two types T and R so T is the your argument what type it
is going to be R is your return type so see how I am implementing function this one so it accepts two generic type so I pass integer so this would be your integer argument it will accept return type would be of a string okay and the object reference now it accept one parameter so you have to provide the integer right so integer certain data okay then argument starts Lambda expression starts provide the implementation and you can process it like so whatever the number I am getting I'm just converting it to a string so num.2 string
and return this output so what I'm returning I am returning a string okay so you can use this functional interface whenever you accept and return also so there is one more type of predicate predicate is like accept but always return Boolean except but always return Boolean represent function that accept one argument and Returns the Boolean okay so I think now can you implement it without even looking right uh it's a very simple right so pause it here just try to implement this right so here it accept an integer so you have it would be like
integer any value implemented Lambda expression let's say if you have more than one line it block start block and semicolon now whatever you have process it and it should return the Boolean ultimately true false whatever it would be so predicate uh whatever the type generic so let's say whatever the input parameter let's say I want to pass the integer is even or not so I am passing one argument here now Lambda expression start my block I'm just checking whether whatever the value I have entered is it an integer or not so a basic method if
it's a equals to equals to 0 return true else false that's all okay and how you can call it is E1 whatever the object reference you have created called DOT test method right so you know right why we are not writing test here that's what Lambda expression resolve internally does it knows that this functional interface has only one abstract method we don't have to write test over here it is very very similar to Anonymous only very very similar to Anonymous only but in Anonymous we have to provide the method name but Lambda expression says that
I work only on functional interface I work only Lambda expression can only work with functional interface functional interface has only one abstract method so that's where we don't have to provide the method name otherwise it is very very similar to Anonymous implementation you got this one so these are the four types consumer supplier function predicate right so if you think that apart from this four you have certain other requirement then you can write your own functional interface but if you think that your requirement gets satisfied with this input our return type you can use already
existing whatever it is available okay if you think that no no I have a total different requirement I have four or five parameters to be returned entered and need to return or not return so then you can write your own function interface okay now let's see you know that interface can extend each other so we just need to see that uh how to handle that use case so let's say use case one functional interface extended non-functional interface so this is my non-functional interface basic living thing interface it has only one abstract method can breathe it
is extended by a functional interface interface bird extends living thing and it also put one abstract method so now here if you see that one abstract method here one abstract method is here so generally all the parent abstract method is also carry forward right so this that's why it show compilation error because functional interface should have only one abstract method but because of this extent any abstract method which is present into the parent interface that also get added to here so that's why it's so compilation error this is wrong but this is right so now
let's say interface has living thing has default right and now I have a functional interface which extends this living thing this works fine because it has only one abstract method this is nothing but a default so it can have a default method okay Second Use case interface extending functional interface so now I have a functional interface first living thing which is the functional interface means only one abstract method should be there it has only one abstract method now I have an interface but it is not a functional interface it's a normal interface it extends living
thing it also have one abstract method but since it extends this abstract also comes to it but this is fine because since we haven't put at the rate function interface above bird so we are not restricting that you should have only one abstract method so you have one and one is coming from your parent extend so even though you can have two but now you are not a functional interface right so this will work fine because this is we haven't put the at the red keyword so it can have more than one abstract method so
this will work fine third use case functional interface extending other function interface so see this I have a functional interface living thing have one abstract method I have another function interface but extending from this it is also have another abstract method see it has can breathe it has can fly different method so means this is one abstract method for this one and from parent also this is also coming to it so it has two abstract method which is wrong but see this one if both the functional interface so this is your parent living thing this
is your child this extends the living thing if both have the same method public Boolean can breathe public uh sorry uh yeah by default is public only public Boolean can breathe both has the same method right then this is allowed this is just like you overrided it right so this is allowed but different is not allowed even when the same signature is allowed and how you can implement it I think you can do it right so now let's say bird interface I have to implement so you have to create a bird reference object any object
where you will hold it it is taking empty so empty I don't have to pass the parameter Lambda expression start can breathe returns Boolean right so let's say I don't have to write a block I just need to return true so just put true finish right and with this object you can call this method can breathe object dot can breathe right so I have uh this object is my eagle Lambda this is start no input parameter Lambda expression start simply returns to can breathe so this is clear right so this is uh so generally when
the two in function interface or one is extended from other if they both have the same method then there is no issue because you can easily implement it will override it but if it is a different then it's an issue so this is pretty much of our functional interface and Lambda expression I think you are just practice it once and I think you should be now clear like why Lambda expression is always always comes and used with functional interface because one simple answer before we're going Lambda expression is a way to implement a functional interface
it is minimizing the verbose which is there in the let's say in the previous phase you don't have to because functional interface only has one method abstract method it don't have to write a method name and all so that's how it is able to reduce the verbose right okay guys so just try to uh implement it once and see is this all getting properly clear to you and any doubt please feel free to ping me in the comment okay guys see you bye