For Episode 61 of the Java Series: https://youtu.be/tv7WcVqJ8X0
//Functional Interface: an interface with only one abstract method. Must be a non-default, non-static, non-private method.
//That's called being implicitly abstract. You can add the abstract modifier if it makes you feel better.
interface FunctionalInterface{
int giveNumber();
}
public class Main {
public static void main(String[] args) {
//The lambda expressions will form the implementation of the abstract method.
//Lets work with one way, in which we assign it to a variable reference
FunctionalInterface interface1;
interface1 = () -> 123; //We have now defined the behavior of the giveNumber method in terms of the interface1 object.
//Technically, we have turned this small piece of code here into its own object and we can call upon it whenever we want by using the object and its method.
System.out.println(interface1.giveNumber()); //Calling upon our new lambda expression
//We can redefine the lambda expression
interface1 = () -> 123 * 5;
System.out.println(interface1.giveNumber());
//Keep in mind the expression must match our abstract method in terms of return type and parameter types
interface1 = () -> "booty"; //wont work because we are trying to return a string
}
}
interface FunctionalInterface{
//We use parameters this time in our abstract method
double giveResult(double a, double b);
}
public class Main {
public static void main(String[] args) {
//Think of some code that can match our abstract method
FunctionalInterface adding = (double a, double b) -> a + b; //You can keep the parameters unspecified too, then it will be inferred
System.out.println(adding.giveResult(5.0, 6.0));
//Let's define another implementation in which we subtract
FunctionalInterface subtracting = (a, b) -> a - b;
System.out.println(subtracting.giveResult(7.0, 2.0)); //Param types must match abstract method(ints can be used because they will basically be autoboxed to a double)
}
}