Java 8 Lambda Basics 9 - Lambdas vs Interface Implementations

Java 8 Lambda Basics 9 - Lambdas vs Interface Implementations

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 173

Number of words: 1188

Number of symbols: 5481

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
now I want to talk about the difference between this thing here which is the hollow while treating and the smile and the function here both of them are of type greeting right let me get rid of this greet method or maybe put this way below so look at these two lines line 13 and line 14 line 13 is basically an instance of a greeting interface which is a custom class that we have written we've written this lol greeting which
00:30
implements the leading interface line 14 is al and expression there's a lambda expression and we've learned that a type of a lambda expression is basically an interface that has a method of that type so since this match is here we have of type rating and the compiler is really happy now the question is what's the difference between this and this what's the difference between hello world greeting and let me change the name for this thing lambda greeting right what are they the same are they different
01:01
hello while greeting is an instance of a specific implementation which also happens to do the exact same thing which just happens to print hello world so are they the same now we know that we can do call a method on top of lol greeting called perform and this should print hello world to the console if I execute this it's gonna print hello well to the console now let me try doing the same thing for the line that we did if I were
01:36
to use this what's gonna happen turns out we still get hello well printed to the console so what's happening is this lambda expression is kind of behaving like an implementation of this interface what we have here is an actual function so in a way we are implementing an interface by just implementing the function and not implementing a class okay that was the difference here between lines 13 and 14 in line 13 we created a new class that implemented the
02:10
greeting interface and then we provided the logic in that however inline 14b not creating a class for it we're just creating a function a lambda expression inline and that is what's getting assigned to a type greeting so it's almost as if we have created an implementation of the greeting interface now how can be sure that it's not actually an implementation well the thing is there are certain things which make the lambda greeting different from the Hallowell greeting
02:41
and what those differences are we'll have to look at it a little bit later but trust me on that this is an instance of a class that implements the greening interface and this is a lambda expression of type cleaning interface there is a difference there the difference is hard to find though because if you think about it what we did here in the hello world greeting we don't really have to create a brand new class we could have done what's considered an anonymous inner class I
03:12
hope you're familiar with what that for an anonymous inner classes let me give you an example it's like create a new greeting instance in your class greeting and what this is gonna do is it's not going to create an instance of another class it's going to create an inline instance of the greeting that the implementation being in line right here so let's say I do this new greeting and I'm going to define the implementation right here so
03:42
I have this public white perform and then I'm going to do a print inside this so what I have here is an inline implementation of an interface this is commonly referred to as an anonymous inner class it's a class that we are defining inside another class so it's an inner class and it doesn't have a name so it's an anonymous inner class and
04:13
this one set of lines is actually doing a couple of things it's creating an implementation of creating which has this method this is the code that needs to run and then it's also creating a new instance by using and inner class creating variable isn't out containing that instance so let's see what happens here if I were to do an anonymous in that class in reading that perform what is it going to display but it's still going to do hello world so we don't really need this let me actually get rid of the olive-oil greeting so you
04:45
have two types of implementations over here one is the lambda greeting where you have the lambda alone and then you have the inner class greeting which is basically doing all this stuff so it's very tempting to say well what we're doing over here is basically just syntactical sugar it's basically another way of creating this kind of an anonymous inner class because that's what's happening right we're creating an instance of treating just like we're doing over here it's just that it's a fancy new shortcut in java 8 the for the
05:16
most purposes you can think of lambda expressions as a shortcut for creating these kind of anonymous inner classes but it's not exactly true there are things that the inner class 'end does which is different from what these lambda expressions do so it's not exactly creating an anonymous inner class we'll look at those differences in a further tutorial but be careful of the fact that you're looking at a completely different thing I see a lot of online tutorials which talk about land are
05:47
starting up as it being some kind of a shortcut for doing anonymous inner classes that is unfortunately not the case lambda is its own thing and you'll learn more about that later but for now think of this as another way of doing something like this instead of doing this approach which is creating this inner class and having an implementation you create a lambda expression like this and of this perform method doesn't have to be over here I can pass either of these to something like a greet method
06:18
because it's of type greeting so any function any method that already accepts this interface works just fine in the in the method drill really doesn't care about whether it's a lambda or an inner class just to prove that I'm going to call creator dot greet for the lambda greeting and liter dot greed for the inner class waiting and these two lines should print
06:48
to other walls as it does so I understand that what I'm doing in this tutorial is telling you that they both look very similar but I'm also saying trust me they are not the same I just wanted to contrast these two layers of creating implementations of an interface one is using something like this another is creating a lambda expression which happens to be off type interface it's not really a way to create an anonymous inner class and the details are going to come much later

DOWNLOAD SUBTITLES: