Java 8 Lambda Basics 5 - Passing Behavior in OOP

Java 8 Lambda Basics 5 - Passing Behavior in OOP

SUBTITLE'S INFO:

Language: English

Type: Robot

Number of phrases: 123

Number of words: 871

Number of symbols: 3783

DOWNLOAD SUBTITLES:

DOWNLOAD AUDIO AND VIDEO:

SUBTITLES:

Subtitles generated by robot
00:00
having said that let's look at some of the problems in object-oriented programming and the kind of constructs you can learn available in java 7 and let's look at how functional programming tries to solve those problems how the new features in Java 8 tries to solve those problems in object-oriented programming pretty much everything is an object you cannot have like a piece of logic a piece of functionality that exists in isolation it has to be a part of a class a part of an object has to be associated with it
00:31
right this is most of the times not a problem but it can be a problem sometimes because for a Java developer when you're designing your system when you're trying to solve a problem and creating you know writing your code you tend to think in terms of things and nouns rather than actual and verbs right it's always a theme for instance let's say you want to write a method that greets buddy you know just writes to the console hello world right you cannot just put a function in there in isolation on its own you have to make it a part of a class right you have to
01:03
create a greeter class which contains that one function you can't have a function just be in isolation now what if somebody was to say okay I don't need a class I just need this one piece of logic one piece of code you cannot do that in Java 7 let me illustrate this with an example so let's say I want to create a new class I'm going to call this greeter and I'm going to create this in the package I hope that our brains let's say one the
01:35
main method is well now this greeter I would have like a greet method so let's say I do a public while greet which just prints something to the console system door art lol simple enough program now I'm going to say Twitter
02:10
there's a new greeter and I'm going to call that method greeter dot creat fairly simple if I execute this I should get hello world printed in the console now look at what this method is doing it is always printing hello world to the console now what I want this greet method to do is not just print hello world to the console all the time I wanted to take input and do different
02:42
things based on the input and input argument right how do I do that in Java I want to accept an argument and that argument lets the grid method know what to do right one way to do this is how the greet method contain all possible combinations of all it can do and have the input argument be a switch it says okay do this thing versus do that thing and then everything is available in the greet method and based on that switch it knows exactly what to do but it's not elegant design what we want to do is have the behavior itself be passed as an
03:14
argument and the greet method doesn't really contain any behavior it just takes the behavior and then it performs it it executes it okay one way to do this in Java 7 and we're talking about you have 8 now for me to do this in the earlier versions of Java is by creating a class or an interface called greeting okay you create an interface called greeting which has a perform method and then you pass to the Greve method an instance of this creating interface when you create an implementation of the
03:46
greeting interface which has a particular implementation of the perform method so let me create this interface and this is gonna have a single method public it performed now that I have this interface I can send different kind of implementations to this thing to the Greek method and have this to that
04:15
instead I'm going to say greeting dot perform of course this will not work we comment this out but now what am I doing here I'm having the grid method accept the behavior and then perform that thing so let me actually create an implementation of the greeting let's say followers greeting and this implements three thing and now
04:50
this has a perform method the disappearance of the world to the console so now in my greeter I can call this greet by creating an implementation of the screeding which is the hollow while greeting so I'm going to do that now I'm going to say hello while greeting new hello well critic and I'm going to pass hello world breathing
05:20
through the greet method now if I run this I should get pretty much the same output but notice what I'm doing here I am passing in a behavior to the greet method I can easily create a new implementation of the greeting interface and have this do something else and if I were to pass that this greet method is going to do with that thing this is classic object-oriented programming this is polymorphism in action right here

DOWNLOAD SUBTITLES: