2- Functional Interfaces
Jun 26, 2026 22:45
· 3:51
· English
· Whisper Turbo
· 2 Konekte
Transkript sa a ekspire jodi a.
Ajoutè pou depo perpétuel →
Montre sèlman
0:03
S…
Speaker 2 (2- Functional Interfaces)
Let's start this section by defining functional interfaces.
0:06
S…
Speaker 2 (2- Functional Interfaces)
A functional interface is an interface with only a single
0:10
S…
Speaker 2 (2- Functional Interfaces)
abstract method.
0:11
S…
Speaker 2 (2- Functional Interfaces)
So earlier we talked about the comparable interface.
0:14
S…
Speaker 2 (2- Functional Interfaces)
This interface has a single abstract method called compareTo.
0:19
S…
Speaker 2 (2- Functional Interfaces)
So this is a functional interface.
0:21
S…
Speaker 2 (2- Functional Interfaces)
We also talked about the comparator interface.
0:24
S…
Speaker 2 (2- Functional Interfaces)
That is a functional interface as well.
0:26
S…
Speaker 2 (2- Functional Interfaces)
Now in this video,
0:27
S…
Speaker 2 (2- Functional Interfaces)
I'm going to create a functional interface from scratch.
0:29
S…
Speaker 2 (2- Functional Interfaces)
We're going to use this functional interface throughout this section.
0:33
S…
Speaker 2 (2- Functional Interfaces)
So in this package lambdas,
0:35
S…
Speaker 1 (2- Functional Interfaces)
I'm going to add a new class Let's call this printer and
0:40
S…
Speaker 1 (2- Functional Interfaces)
change the kind to interface So this
0:46
S…
Speaker 2 (2- Functional Interfaces)
is the interface of a printer in one implementation We may want to print on the
0:50
S…
Speaker 2 (2- Functional Interfaces)
terminal in another implementation We may want to talk to an actual
0:54
S…
Speaker 2 (2- Functional Interfaces)
printer so we could have different implementations of this interface Now
0:58
S…
Speaker 2 (2- Functional Interfaces)
what operations do we need here?
1:00
S…
Speaker 2 (2- Functional Interfaces)
We need a single method for printing a message.
1:03
S…
Speaker 1 (2- Functional Interfaces)
So void print message.
1:07
S…
Speaker 2 (2- Functional Interfaces)
Now we have an interface with a single abstract
1:11
S…
Speaker 1 (2- Functional Interfaces)
method.
1:11
S…
Speaker 2 (2- Functional Interfaces)
So this is a functional interface.
1:13
S…
Speaker 2 (2- Functional Interfaces)
Now in the second part of this series,
1:15
S…
Speaker 2 (2- Functional Interfaces)
I told you that in the recent years,
1:17
S…
Speaker 2 (2- Functional Interfaces)
Java introduced the concept of default methods in interfaces.
1:21
S…
Speaker 2 (2- Functional Interfaces)
These default methods can have implementation.
1:23
S…
Speaker 2 (2- Functional Interfaces)
So we can have a default method like this default void
1:28
S…
Speaker 2 (2- Functional Interfaces)
print twice,
1:29
S…
Speaker 1 (2- Functional Interfaces)
which takes a message and prints it twice.
1:34
S…
Speaker 1 (2- Functional Interfaces)
So we can have code,
1:35
S…
Speaker 2 (2- Functional Interfaces)
we can have implementation in an interface.
1:37
S…
Speaker 2 (2- Functional Interfaces)
I told you that in my opinion,
1:39
S…
Speaker 2 (2- Functional Interfaces)
this is a bad approach that the Java team has taken because interfaces
1:43
S…
Speaker 2 (2- Functional Interfaces)
should not have implementation.
1:45
S…
Speaker 2 (2- Functional Interfaces)
They should only be used as contracts.
1:47
S…
Speaker 1 (2- Functional Interfaces)
But that aside,
1:48
S…
Speaker 2 (2- Functional Interfaces)
what I want to point out here is that this interface has two methods.
1:52
S…
Speaker 2 (2- Functional Interfaces)
One of them is a default method with implementation.
1:55
S…
Speaker 2 (2- Functional Interfaces)
The other is an abstract method.
1:57
S…
Speaker 2 (2- Functional Interfaces)
This interface is still a functional interface because it has a single
2:01
S…
Speaker 2 (2- Functional Interfaces)
abstract method.
2:02
S…
Speaker 2 (2- Functional Interfaces)
So it doesn't matter if you have many default or even static methods in
2:07
S…
Speaker 2 (2- Functional Interfaces)
our interfaces.
2:07
S…
Speaker 2 (2- Functional Interfaces)
As long as we have a single abstract method,
2:10
S…
Speaker 2 (2- Functional Interfaces)
we refer to that interface as a functional interface.
2:12
S…
Speaker 1 (2- Functional Interfaces)
Now,
2:13
S…
Speaker 2 (2- Functional Interfaces)
let me delete this ugly default method.
2:16
S…
Speaker 1 (2- Functional Interfaces)
Alright,
2:17
S…
Speaker 2 (2- Functional Interfaces)
so let's go ahead and implement this interface in a class.
2:21
S…
Speaker 1 (2- Functional Interfaces)
I'm going to add a new class called
2:25
S…
Speaker 2 (2- Functional Interfaces)
console printer.
2:28
S…
Speaker 2 (2- Functional Interfaces)
In this implementation,
2:30
S…
Speaker 2 (2- Functional Interfaces)
we're going to print on the console or terminal.
2:32
S…
Speaker 2 (2- Functional Interfaces)
So let's have this class implement the printer interface.
2:36
S…
Speaker 1 (2- Functional Interfaces)
Here we're going to add a print statement
2:40
S…
Speaker 1 (2- Functional Interfaces)
and print this message.
2:42
S…
Speaker 1 (2- Functional Interfaces)
Pretty straightforward,
2:43
S…
Speaker 1 (2- Functional Interfaces)
right?
2:44
S…
Speaker 1 (2- Functional Interfaces)
Now,
2:44
S…
Speaker 2 (2- Functional Interfaces)
back to our demo class.
2:47
S…
Speaker 2 (2- Functional Interfaces)
Here I'm going to create a static method,
2:49
S…
Speaker 2 (2- Functional Interfaces)
public static.
2:51
S…
Speaker 2 (2- Functional Interfaces)
I'm using static so I can easily call it from this other static method.
2:55
S…
Speaker 2 (2- Functional Interfaces)
It doesn't have to be static.
2:56
S…
Speaker 1 (2- Functional Interfaces)
So public static void greet.
3:00
S…
Speaker 1 (2- Functional Interfaces)
this method needs a printer.
3:02
S…
Speaker 2 (2- Functional Interfaces)
So here we're going to talk to the printer interface,
3:05
S…
Speaker 2 (2- Functional Interfaces)
we don't care about the implementation,
3:06
S…
Speaker 2 (2- Functional Interfaces)
we are programming to an interface,
3:08
S…
Speaker 1 (2- Functional Interfaces)
okay?
3:09
S…
Speaker 2 (2- Functional Interfaces)
So printer,
3:10
S…
Speaker 1 (2- Functional Interfaces)
then we call printer dot print,
3:14
S…
Speaker 2 (2- Functional Interfaces)
and here we can say hello world.
3:16
S…
Speaker 1 (2- Functional Interfaces)
Now in our show method I'm going to call the greet
3:21
S…
Speaker 2 (2- Functional Interfaces)
method, here we need to pass an object that implements the printer interface,
3:25
S…
Speaker 2 (2- Functional Interfaces)
so we can use our console printer object.
3:28
S…
Speaker 1 (2- Functional Interfaces)
Now when we run this program,
3:32
S…
Speaker 2 (2- Functional Interfaces)
we see the hello world message.
3:33
S…
Speaker 1 (2- Functional Interfaces)
So pretty straightforward.
3:34
S…
Speaker 1 (2- Functional Interfaces)
Now,
3:35
S…
Speaker 2 (2- Functional Interfaces)
sometimes we don't want to explicitly create a class to implement an interface because
3:39
S…
Speaker 1 (2- Functional Interfaces)
this requires writing a bit of code.
3:41
S…
Speaker 2 (2- Functional Interfaces)
Sometimes we may not want to reuse this class.
3:44
S…
Speaker 1 (2- Functional Interfaces)
In the next video,
3:45
S…
Speaker 2 (2- Functional Interfaces)
I'm going to show you how to use anonymous inner classes for this purpose.
Transkript sa a te kreye pa AI (automatic speech recognition). Li ka gen erè — tcheke ak son orijinal la pou itilize kritik. Politik AI
Resume
Klike sou Remak pou kreye yon remak AI de transkript sa a.
Enstale...
Konbyen tan pou m' fè transkript la?
Mande nenpòt bagay sou transkript sa a - AI a pral jwenn seksyon ki enpòtan yo epi reponn.