What is a Functional Interface?
45sClear, concise explanation of a key Java concept with practical examples, perfect for beginners.
▶ Play Clip"Delivers exactly what the title promises: a thorough, well-structured tutorial on Java interfaces with practical examples."
This video is a tutorial on Java interfaces, focusing on functional interfaces, anonymous inner classes, lambda expressions, and method references. The instructor, Mosh Hamedani, explains these concepts with practical examples, showing how to implement interfaces and use modern Java features to write cleaner, more concise code.
A functional interface is an interface with only a single abstract method. Examples include Comparable and Comparator interfaces.
The instructor creates a 'Printer' interface with a single abstract method 'print(String message)' and a default method 'printTwice'.
An interface can have default and static methods and still be functional as long as it has only one abstract method.
A 'ConsolePrinter' class implements the Printer interface, providing a concrete implementation for the print method.
Anonymous inner classes allow implementing an interface without creating a separate class, useful for one-time use.
Lambda expressions are anonymous functions that can be passed around, providing a more concise way to implement functional interfaces.
The syntax includes parameter list, arrow operator, and body. Type inference allows omitting parameter types, and parentheses can be omitted for single parameters.
Lambda expressions can be stored in variables of the functional interface type, acting as objects representing anonymous functions.
Lambdas can access local variables, static fields, and instance fields of the enclosing class. 'this' refers to the enclosing object, unlike anonymous inner classes.
Method references provide a shorthand for lambdas that simply call an existing method. Syntax: ClassName::methodName or object::methodName, and ClassName::new for constructors.
The video effectively demonstrates how to use functional interfaces, anonymous inner classes, lambda expressions, and method references in Java to write cleaner and more concise code. It emphasizes the importance of understanding these concepts for modern Java development.
What is a functional interface?
An interface with only a single abstract method.
00:33
Can an interface have default methods and still be functional?
Yes, as long as it has only one abstract method.
02:38
What is an anonymous inner class?
A class without a name that implements an interface or extends a class, used for one-time implementations.
04:43
What is a lambda expression?
An anonymous function that can be passed around, used to implement functional interfaces concisely.
05:49
In a lambda expression, what does 'this' refer to?
The enclosing object, unlike anonymous inner classes where 'this' refers to the inner class instance.
10:30
What is a method reference?
A shorthand for a lambda expression that calls an existing method, using the syntax ClassName::methodName.
11:44
How do you reference a constructor using a method reference?
Using ClassName::new.
14:45
Functional Interface Definition
Establishes the core concept that functional interfaces have exactly one abstract method, which is essential for lambdas.
00:33Lambda Expressions Introduction
Introduces the key feature that enables functional programming in Java, making code more concise.
05:49Lambda 'this' vs Anonymous Inner Class
Highlights a subtle but important difference that affects how you access enclosing instance members.
10:30Method References
Shows how to further simplify lambdas, improving readability and reducing boilerplate.
11:30[00:02] everything you need to know about java interfaces so by the end of this video confidence hi i'm mash hamadani and i've taught millions of people how to code through this channel and my online school
[00:15] codewoodmash.com this video is part of my ultimate java mastery course so once learn more you may want to look at the complete course now let's jump in and complete course now let's jump in and get started
[00:33] functional interfaces a functional interface is an interface with only a single abstract method so earlier we talked about the comparable interface this interface has a single abstract method called
[00:47] compare2 so this is a functional interface we also talked about the comparator interface that is a functional interface as well functional interface from scratch we're going to use this functional interface
[01:00] so in this package lambdas i'm going to add let's call this printer and change the kind to interface
[01:16] one implementation we may want to print on the terminal in another implementation we may want to talk to an actual printer so we could have interface now what operations do we need here we
[01:29] need a single method for printing a message so message now we have an interface with a single interface now in the second part of this series i
[01:44] told you that in the recent years java introduced the concept of default methods in interfaces these default methods can have implementation so we can have a default method like this default void print twice which takes a
[01:58] prints it twice so we can have code we can have implementation in an interface i told you that in my opinion this is a bad approach that the java team has taken
[02:11] because interfaces should not have implementation they should only be used as contracts but that aside what i want to point out here is that this interface has two methods one of them is a default method with implementation the other is
[02:24] an abstract method this interface is still a functional interface because it has a single abstract method so it doesn't matter if we have many default or even static methods in our interfaces as long as we have a single abstract
[02:38] functional interface method all right so let's go ahead and implement this interface in a class
[02:51] i'm going to add a new class called console printer in this implementation we're going to print on the console or terminal so let's have this class implement the printer interface
[03:08] statement and print this message pretty straightforward right now back to our demo class here i'm going to create a static method public static i'm using static so i can
[03:21] easily call it from this other static method it doesn't have to be static so public static void grid this method needs a printer so here interface we don't care about the implementation we are programming to an
[03:36] interface okay so printer then we call printer dot print and here we can say hello world now in our show method i'm going to call the greet method
[03:50] here we need to pass an object that implements the printer interface so we can use our console printer object now when we run this program we see the hello world message so pretty
[04:02] straightforward now sometimes we don't want to explicitly create a class to implement an interface because this requires writing a bit of code sometimes we may not want to reuse this class in the next video i'm going to show you
[04:14] how to use anonymous inner classes for this purpose so in the last video we passed a new instance of our console printer class to
[04:29] now as i told you sometimes we don't want to explicitly create a class to implement an interface perhaps we want to implement an interface only once and use it for a single purpose we don't want to reuse that class so that's where
[04:43] we use anonymous inner classes so instead of creating a new instance of the console printer class here we can type new printer so we type the name of the interface and press enter
[04:56] what we have here is called an anonymous inner class it's anonymous because this class doesn't have a name it's just an implementation and we call it an inner class because we're using this inside a method okay so
[05:12] print method here we can use a print statement to print this message now when we run this program we get the exact same result so this is how we can use an anonymous inner class
[05:29] with anonymous inner classes we can achieve the same result by writing less code but java 8 introduced a better and more concise way to achieve the same result that's called a lambda expression and we're going to look at that next
[05:49] so our greet method talks to the printer interface and this is a functional interface because it has a single abstract method now wouldn't it be nice if we could implement this method in a standalone
[06:01] function like a function that exists on its own without belonging to a class that is what lambda expressions are for so a lambda expression is like an anonymous function that we can pass around let me show you so i'm going to
[06:14] expression first recall now here we want to pass an anonymous function so
[06:27] this print method has a single parameter of type string let's add that over here this is where we list our parameters so string message after parenthesis we add an arrow this
[06:40] is called the lambda operator and then we add braces to represent the body of this function now what should we do here we should add a print statement and print this message on the console
[06:55] what we have over here is called a lambda expression so if we have a functional interface we can represent this functional interface using a lambda expression that is why these interfaces are called functional because they
[07:07] represent a function so here we have a lambda expression as you can see this code is very clean and concise but we can make it better first of all we can remove the type of this
[07:19] parameter now the java compiler knows that this parameter is a string object because the lambda expression that we pass over here will be checked against the signature of the print method so the java compiler
[07:32] knows that this method has a single parameter and the type of this parameter is string so we don't have to repeat that type over here if i type message dot now we can see all the string methods
[07:45] by the same token if i add another parameter here the java compiler complains because it knows that our print method has a single parameter but here we're passing two parameters okay
[07:57] so for the most part we can exclude the type of parameters and let the java compiler infer them now if we have a single parameter we can also remove this parenthesis that makes our code cleaner and more
[08:10] concise we use parentheses only if we have no parameters or if we have a method with multiple parameters so we separate these parameters using a comma of a method okay
[08:24] a single parameter and we don't need parenthesis also if the body of this function has a single line of code we don't need these so i can clean up this code by pressing alt on enter and selecting replace with
[08:39] expression lambda there you go so this download expression looks really clean and concise compare this with an ugly so let me remove this
[08:54] all right now here we're passing a lambda expression as an argument to a method but we can also store a lambda expression in a variable for example we can declare a variable of type printer
[09:06] and set it to a new console printer so this is the concrete implementation or we can set this to a lambda expression so here we say message goes to print message
[09:20] so lambda expressions are essentially objects but we can use them to represent objects but we can use them to represent anonymous functions
[09:33] in the body of this lambda we're using the message parameter that we have access the local variables in the enclosing method so i'm going to declare a variable called prefix and set it to hyphen
[09:47] here we can reference prefix and then append the message so when we run we get hyphen hello world we can also access the static fields and the enclosing class so i'm going to move this from here and declare it as a
[10:03] static field in this class public static string prefix we set it to hyphen now we can access it in our lambda expression so when we run we get the same result as before
[10:16] we can also access the instance fields so i'm going to make this an instance and also make the show method an instance method so as you can see we can instance method so as you can see we can access the prefix field now what about
[10:30] what do you think this represents here it represents the current instance of the lambda's demo class so if we type dot you can see the prefix field and the show method so this is one of the differences between lambda
[10:45] expressions and anonymous inner classes in lambda expressions this references the enclosing object whereas in anonymous inner classes this references inner class another difference between
[11:00] classes can have state so they can have fields to store some data in lambda expressions we cannot have fields because this lambda expression is just representing an anonymous function so we cannot have instance
[11:14] fields here we cannot have state we can only access the local variables declared in the enclosing object as well as the static and instance fields in the static and instance fields in the enclosing class
[11:30] sometimes all we do in a lambda expression is passing the parameter or parameters to an existing method for example over here we're simply passing this object in these cases it's easier to reference
[11:44] in these cases it's easier to reference this method directly let me show you so here's the syntax we type the name of the class or the object that contains this method then we type double colons followed by
[11:56] parenthesis because we don't want to call this method we just want to add a reference to it for example if i want to rewrite this lambda expression using a method reference i would type greet
[12:10] now what is the object that contains the print line method it's this object right so we type system.out then we add double colons followed by the name of the method
[12:22] without parenthesis so this is a method reference and this code is identical to what we have over here now with intellij we can always convert these kind of lambda expressions to a method reference very easily so we put
[12:36] the carrot on the body of the lambda press alt enter and then replace lambda with metal reference there you go now with these method references we can reference static or instance methods in the class as well as constructors let me
[12:51] show you a few examples so i'm going to delete these and add a static method in this class public static void print message now the signature of this method matches
[13:05] interface okay so here we call the grid method we should pass a printer object so here we can use a lambda expression message we take it and pass it to this
[13:18] method right again all we're doing here is passing the parameter to an existing method so we can use a method reference we type the name of the class that contains this method in this case lam
[13:32] does demo so lambda's demo of the method there you go there you go or you can convert this using intellij
[13:44] now what if this was an instance method let's see how it works so i'm going to create a lambdas demo object demo we set it to a new instance of the lambdas demo class
[13:59] now we want to call the grid method and pass a lambda expression like this goes to demo dot here we want to call this instance method so print message now to use a method reference
[14:13] contains this method demo double colons and print if we use intellij we get the exact same now what about passing a value to a constructor let's take a look so delete
[14:30] constructor let's take a look so delete i'm going to add a constructor here string parameter now let's say we call the greet method and pass a message to the constructor of
[14:45] and pass a message to the constructor of the lambda's demo class we type the name of the class that is lambda's demo double colon and here we type new to represent the constructor
[15:02] if we use intellij we get the exact same result so with method references we can write compact and easier to read lambda compact and easier to read lambda expressions
[15:15] video as i said this video is part of my ultimate java mastery course that about java from the basics to more advanced concepts so if you want to learn more i highly encourage you to take a full course it's much faster than
[15:29] jumping from one tutorial to another if you're interested the link is below this you're interested the link is below this video thank you and have a great day
[15:41] [Music] you
⚡ Saved you 0h 15m reading this? Transcribe any YouTube video for free — no signup needed.