Uitsluitend tonen
0:03
S… Speaker 2 (6- Constraints)
There are times you want to add a constraint or a restriction on a
0:07
S… Speaker 2 (6- Constraints)
type parameter.
0:08
S… Speaker 1 (6- Constraints)
For example,
0:09
S… Speaker 2 (6- Constraints)
let's say we only want to store numbers in this list.
0:11
S… Speaker 2 (6- Constraints)
Perhaps this list is going to support some operations that only make sense
0:16
S… Speaker 2 (6- Constraints)
for numbers.
0:16
S… Speaker 2 (6- Constraints)
To do that here,
0:17
S… Speaker 2 (6- Constraints)
we type extends number.
0:20
S… Speaker 1 (6- Constraints)
Now,
0:22
S… Speaker 2 (6- Constraints)
T can only be the number class or any of its child classes.
0:26
S… Speaker 2 (6- Constraints)
But what is this number class?
0:28
S… Speaker 1 (6- Constraints)
Well,
0:28
S… Speaker 2 (6- Constraints)
if you search for Java number class,
0:31
S… Speaker 2 (6- Constraints)
here you can find the documentation for this class.
0:33
S… Speaker 2 (6- Constraints)
Let's look at the documentation for java platform standard edition
0:38
S… Speaker 2 (6- Constraints)
version 8.
0:38
S… Speaker 1 (6- Constraints)
So,
0:40
S… Speaker 2 (6- Constraints)
here you can see that the number class is declared in java .lang
0:45
S… Speaker 2 (6- Constraints)
package, and it's the base class for these classes,
0:48
S… Speaker 2 (6- Constraints)
like byte,
0:49
S… Speaker 2 (6- Constraints)
double,
0:50
S… Speaker 2 (6- Constraints)
float,
0:50
S… Speaker 2 (6- Constraints)
integer,
0:51
S… Speaker 2 (6- Constraints)
long, and so on.
0:52
S… Speaker 2 (6- Constraints)
So all these wrapper classes around numeric primitive types derive
0:56
S… Speaker 2 (6- Constraints)
from the number class.
0:58
S… Speaker 2 (6- Constraints)
So with this constraint,
1:00
S… Speaker 2 (6- Constraints)
if we create a new list of string,
1:03
S… Speaker 2 (6- Constraints)
we get a compilation error because this string is not
1:07
S… Speaker 2 (6- Constraints)
a number.
1:07
S… Speaker 2 (6- Constraints)
So here we can only pass the number class or any class that derives
1:12
S… Speaker 2 (6- Constraints)
from the number, like integer or float or short.
1:16
S… Speaker 1 (6- Constraints)
Also,
1:17
S… Speaker 2 (6- Constraints)
this constraint doesn't have to be a class.
1:19
S… Speaker 2 (6- Constraints)
It can also be an interface.
1:20
S… Speaker 1 (6- Constraints)
For example,
1:22
S… Speaker 2 (6- Constraints)
in Java we have a popular interface called comparable.
1:26
S… Speaker 2 (6- Constraints)
We use this interface for implementing classes that can be compared with each other.
1:30
S… Speaker 1 (6- Constraints)
For example,
1:31
S… Speaker 2 (6- Constraints)
if we implement this interface in our user class,
1:34
S… Speaker 2 (6- Constraints)
we can compare two user objects based on some criteria,
1:38
S… Speaker 2 (6- Constraints)
like their last login date.
1:39
S… Speaker 2 (6- Constraints)
Then we can sort all users based on their last login date.
1:43
S… Speaker 2 (6- Constraints)
We'll look at the comparable interface later in this section.
1:46
S… Speaker 2 (6- Constraints)
But let's see how applying this constraint works.
1:48
S… Speaker 2 (6- Constraints)
So now we're saying that this list can only store objects that
1:52
S… Speaker 2 (6- Constraints)
are comparable.
1:53
S… Speaker 2 (6- Constraints)
So back in the main class,
1:55
S… Speaker 2 (6- Constraints)
We can pass short here because short values are comparable.
1:59
S… Speaker 2 (6- Constraints)
We can also pass integer or string,
2:02
S… Speaker 2 (6- Constraints)
but we cannot pass user because our user class
2:06
S… Speaker 2 (6- Constraints)
does not implement the comparable interface.
2:09
S… Speaker 1 (6- Constraints)
Now,
2:10
S… Speaker 2 (6- Constraints)
if you go to our user class and type implements comparable,
2:14
S… Speaker 2 (6- Constraints)
now don't worry about this compilation error,
2:17
S… Speaker 2 (6- Constraints)
we'll come back and fix this later.
2:19
S… Speaker 2 (6- Constraints)
But we're saying that the user class implements the comparable interface.
2:23
S… Speaker 1 (6- Constraints)
Now,
2:25
S… Speaker 2 (6- Constraints)
you can see that the error is gone in the main method.
2:27
S… Speaker 1 (6- Constraints)
Also,
2:28
S… Speaker 2 (6- Constraints)
here we can add multiple interfaces as constraints.
2:31
S… Speaker 1 (6- Constraints)
For example,
2:32
S… Speaker 2 (6- Constraints)
we can type and clonable,
2:35
S… Speaker 2 (6- Constraints)
this is another popular interface in Java.
2:38
S… Speaker 2 (6- Constraints)
If you want to be able to clone or copy a class,
2:41
S… Speaker 2 (6- Constraints)
we should implement this interface in that class.
2:44
S… Speaker 2 (6- Constraints)
Now we are saying that this generic list can only store objects that are comparable
2:49
S… Speaker 2 (6- Constraints)
and clonable.
2:51
S… Speaker 2 (6- Constraints)
And by the way,
2:52
S… Speaker 2 (6- Constraints)
note that here we have a single ampersand,
2:54
S… Speaker 2 (6- Constraints)
so this is not the logical and operator.
2:56
S… Speaker 1 (6- Constraints)
Now,
2:58
S… Speaker 2 (6- Constraints)
back in the main class,
2:59
S… Speaker 2 (6- Constraints)
we cannot pass the user class here anymore because this class only
3:04
S… Speaker 2 (6- Constraints)
implements the comparable interface.
3:06
S… Speaker 2 (6- Constraints)
We can only pass classes that implement both the comparable and clonable
3:11
S… Speaker 1 (6- Constraints)
interfaces.
3:11
S… Speaker 2 (6- Constraints)
So this is all about applying constraints.
3:13
S… Speaker 1 (6- Constraints)
Now,
3:14
S… Speaker 2 (6- Constraints)
with this constraint,
3:15
S… Speaker 2 (6- Constraints)
we say that we have a bounded type parameter.
3:19
S… Speaker 2 (6- Constraints)
This type parameter,
3:20
S… Speaker 2 (6- Constraints)
this t, is bounded,
3:22
S… Speaker 2 (6- Constraints)
it's restricted.

This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy

❤️ Hou je van STT.ai? Vertel het je vrienden!
Samenvatting
Klik op Summarize om een AI samenvatting van dit transcript te genereren.
Samengevat...
Vraag AI over dit Transcript
Vraag maar iets over dit transcript De AI zal relevante secties en antwoord vinden.