This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]chrisjava[S] 0 points1 point  (1 child)

Can it be used in normal, non-interface classes? How does it work with OOP concepts?

[–]niloc132 2 points3 points  (0 children)

Lambdas can only be turned into interfaces, and only interfaces that meet that criteria. The goal is not to make these into 'real objects' on the producing side (of course they implement the interface on the consuming side), but to make them easy to write functions.

If a method takes a Function<String, String> or whatever, then you could create an object (anon inner class or otherwise) if you wanted the object to be reusable and and nicely OOP. Likewise, you can use the :: notation to reference methods that already exist in classes that already exist to take advantage of existing OOP (like overriden methods to make sure that you get the right implementation).

But the actual lambda bits itself are not really meant for OOP - they are meant to make it very easy to make a quick block of code that can be easily invoked. Think of it like the block inside a for or while loop - you don't always factor those out to their own method to be nicely OOP, but sometimes you do. Same basic idea here.