12- Wildcards
Jun 20, 2026 07:23
· 5:27
· English
· Whisper Turbo
· 2 speakers
Bu transkriptin süresi sona eriyor 24 Günler.
Kalıcı depolama için yükselt →
Sadece göster
0:03
S…
Speaker 1 (12- Wildcards)
A better way to solve the problem in the last video is by using a wildcard.
0:07
S…
Speaker 1 (12- Wildcards)
So we go back to our utils class and replace this
0:11
S…
Speaker 1 (12- Wildcards)
type argument with a question mark which is called the wildcard character.
0:16
S…
Speaker 1 (12- Wildcards)
This wildcard represents an unknown type.
0:19
S…
Speaker 1 (12- Wildcards)
So with this we can go back to the main class and here we can pass
0:23
S…
Speaker 1 (12- Wildcards)
a list of users.
0:23
S…
Speaker 1 (12- Wildcards)
We can also pass a list of instructors.
0:26
S…
Speaker 1 (12- Wildcards)
In fact,
0:27
S…
Speaker 1 (12- Wildcards)
we can pass a list of anything.
0:28
S…
Speaker 1 (12- Wildcards)
For example,
0:29
S…
Speaker 1 (12- Wildcards)
we can pass
0:30
S…
Speaker 1 (12- Wildcards)
a generic list of string objects or integers,
0:35
S…
Speaker 1 (12- Wildcards)
so we can pass a list of anything.
0:36
S…
Speaker 2 (12- Wildcards)
Now,
0:37
S…
Speaker 1 (12- Wildcards)
in this case,
0:38
S…
Speaker 1 (12- Wildcards)
it doesn't quite make sense to pass a list of integers and treat them
0:42
S…
Speaker 1 (12- Wildcards)
as user objects,
0:43
S…
Speaker 1 (12- Wildcards)
but don't worry,
0:44
S…
Speaker 1 (12- Wildcards)
we're going to come back and fix it in a second.
0:45
S…
Speaker 1 (12- Wildcards)
But let me show you something first.
0:47
S…
Speaker 1 (12- Wildcards)
Back to the utils class,
0:49
S…
Speaker 1 (12- Wildcards)
here I'm going to call users .get,
0:52
S…
Speaker 1 (12- Wildcards)
now look at the return type of the get method.
0:55
S…
Speaker 1 (12- Wildcards)
It's returning capture of unknown.
0:58
S…
Speaker 1 (12- Wildcards)
What is this?
0:58
S…
Speaker 2 (12- Wildcards)
Well,
1:00
S…
Speaker 1 (12- Wildcards)
When we use a wild card,
1:01
S…
Speaker 1 (12- Wildcards)
the Java compiler is going to create an anonymous type under the hood.
1:06
S…
Speaker 1 (12- Wildcards)
So it's going to create a class.
1:09
S…
Speaker 1 (12- Wildcards)
Let's call it cap number one.
1:11
S…
Speaker 1 (12- Wildcards)
This is going to be a regular class in Java.
1:14
S…
Speaker 1 (12- Wildcards)
We don't see it,
1:15
S…
Speaker 1 (12- Wildcards)
but it's there.
1:16
S…
Speaker 2 (12- Wildcards)
Now,
1:17
S…
Speaker 1 (12- Wildcards)
why did I use one over here?
1:19
S…
Speaker 1 (12- Wildcards)
Because we could have multiple wild cards here.
1:21
S…
Speaker 1 (12- Wildcards)
Okay?
1:22
S…
Speaker 1 (12- Wildcards)
So the get method returns an instance
1:26
S…
Speaker 1 (12- Wildcards)
of this capture class.
1:28
S…
Speaker 1 (12- Wildcards)
And that means if we get an object here,
1:30
S…
Speaker 1 (12- Wildcards)
we can only store it in a variable of type cap or its
1:34
S…
Speaker 1 (12- Wildcards)
base type.
1:35
S…
Speaker 1 (12- Wildcards)
That is the object class.
1:37
S…
Speaker 1 (12- Wildcards)
So object x equals users that get
1:41
S…
Speaker 1 (12- Wildcards)
zero. So this is what happens when we use a wildcard.
1:44
S…
Speaker 2 (12- Wildcards)
Now,
1:45
S…
Speaker 1 (12- Wildcards)
how can we restrict the lists that are passed here?
1:48
S…
Speaker 1 (12- Wildcards)
We don't want to pass a list of integers or strings.
1:50
S…
Speaker 1 (12- Wildcards)
We want to pass a list of users or its subtypes.
1:53
S…
Speaker 1 (12- Wildcards)
That's very easy to solve.
1:55
S…
Speaker 1 (12- Wildcards)
Let me put this on a new line so you can see clearly.
1:59
S…
Speaker 1 (12- Wildcards)
So here's the parameter of the print users method.
2:01
S…
Speaker 2 (12- Wildcards)
Now,
2:03
S…
Speaker 1 (12- Wildcards)
after this wildcard character,
2:06
S…
Speaker 1 (12- Wildcards)
we can type extends user.
2:08
S…
Speaker 1 (12- Wildcards)
When the Java compiler sees this expression,
2:12
S…
Speaker 1 (12- Wildcards)
it's going to have the capture class extend the user class.
2:16
S…
Speaker 1 (12- Wildcards)
So our capture class is going to look like this.
2:18
S…
Speaker 1 (12- Wildcards)
Exactly like how the instructor class extends
2:23
S…
Speaker 1 (12- Wildcards)
the user class.
2:24
S…
Speaker 1 (12- Wildcards)
Let me put them side by side.
2:25
S…
Speaker 1 (12- Wildcards)
So class instructor also extends the user class.
2:29
S…
Speaker 2 (12- Wildcards)
Right?
2:30
S…
Speaker 1 (12- Wildcards)
Now, back to the main class,
2:32
S…
Speaker 1 (12- Wildcards)
we have a compilation error because we cannot pass a list of integers here.
2:36
S…
Speaker 1 (12- Wildcards)
We can only pass a list of user or subtypes of user.
2:40
S…
Speaker 1 (12- Wildcards)
So we can pass a list of user objects or list of
2:44
S…
Speaker 1 (12- Wildcards)
instructor objects.
2:45
S…
Speaker 1 (12- Wildcards)
These are all valid arguments.
2:47
S…
Speaker 2 (12- Wildcards)
Now,
2:48
S…
Speaker 1 (12- Wildcards)
because this capture class is a subtype of
2:53
S…
Speaker 1 (12- Wildcards)
this user, we can get an object and store it in
2:57
S…
Speaker 1 (12- Wildcards)
a user variable because the user class is the parent
3:01
S…
Speaker 1 (12- Wildcards)
of the capture class,
3:02
S…
Speaker 1 (12- Wildcards)
right?
3:03
S…
Speaker 1 (12- Wildcards)
However,
3:03
S…
Speaker 1 (12- Wildcards)
we cannot assign this object to a variable of type instructor because
3:08
S…
Speaker 1 (12- Wildcards)
instructor and cap are two different types.
3:11
S…
Speaker 1 (12- Wildcards)
It's like assigning an integer to a string.
3:14
S…
Speaker 1 (12- Wildcards)
There are two entirely different classes.
3:16
S…
Speaker 1 (12- Wildcards)
Let's change instructor back to the user.
3:19
S…
Speaker 2 (12- Wildcards)
Now,
3:22
S…
Speaker 1 (12- Wildcards)
what about adding an object to this list?
3:24
S…
Speaker 1 (12- Wildcards)
Let's see what happens.
3:25
S…
Speaker 1 (12- Wildcards)
So users that add,
3:27
S…
Speaker 1 (12- Wildcards)
look at the parameter of this method.
3:30
S…
Speaker 1 (12- Wildcards)
capture of unknown extends user.
3:33
S…
Speaker 1 (12- Wildcards)
So here we can pass an instance of this capture class or
3:37
S…
Speaker 1 (12- Wildcards)
any of its subtypes.
3:38
S…
Speaker 2 (12- Wildcards)
Now,
3:39
S…
Speaker 1 (12- Wildcards)
unfortunately, we do not have access to this capture class,
3:42
S…
Speaker 1 (12- Wildcards)
so we cannot new it up here.
3:43
S…
Speaker 1 (12- Wildcards)
New capture doesn't make sense.
3:45
S…
Speaker 1 (12- Wildcards)
So with the extends keyword,
3:48
S…
Speaker 1 (12- Wildcards)
we can only read from this list.
3:50
S…
Speaker 1 (12- Wildcards)
We cannot add to it.
3:51
S…
Speaker 1 (12- Wildcards)
If you want to add to a list,
3:53
S…
Speaker 1 (12- Wildcards)
we should use the super keyword.
3:56
S…
Speaker 1 (12- Wildcards)
Now, when the Java compiler sees this expression,
3:59
S…
Speaker 1 (12- Wildcards)
it's going to treat this unknown type like the parent of the user class.
4:03
S…
Speaker 1 (12- Wildcards)
What is the parent of the user class?
4:05
S…
Speaker 1 (12- Wildcards)
It's the object class.
4:06
S…
Speaker 1 (12- Wildcards)
So this user's list is going to look like this.
4:09
S…
Speaker 1 (12- Wildcards)
Generic list of object,
4:12
S…
Speaker 1 (12- Wildcards)
let's call it temp,
4:13
S…
Speaker 1 (12- Wildcards)
new generic list.
4:15
S…
Speaker 1 (12- Wildcards)
So the Java compiler is going to treat this object
4:19
S…
Speaker 1 (12- Wildcards)
like this object.
4:20
S…
Speaker 2 (12- Wildcards)
Now,
4:23
S…
Speaker 1 (12- Wildcards)
in this list,
4:24
S…
Speaker 1 (12- Wildcards)
we can add an instance of the object class or any of its subtypes.
4:29
S…
Speaker 1 (12- Wildcards)
So we can type users .add new user.
4:32
S…
Speaker 1 (12- Wildcards)
We can also add a new instructor.
4:35
S…
Speaker 1 (12- Wildcards)
Because all these types directly or indirectly
4:39
S…
Speaker 1 (12- Wildcards)
derive from the object class.
4:41
S…
Speaker 1 (12- Wildcards)
However,
4:42
S…
Speaker 1 (12- Wildcards)
if we use the super keyword,
4:44
S…
Speaker 1 (12- Wildcards)
we cannot read from this list.
4:45
S…
Speaker 1 (12- Wildcards)
So if I type users .get0,
4:49
S…
Speaker 1 (12- Wildcards)
we can store the result in a variable of type object.
4:53
S…
Speaker 1 (12- Wildcards)
If you want to store the result in a variable of
4:57
S…
Speaker 1 (12- Wildcards)
type user,
4:59
S…
Speaker 1 (12- Wildcards)
we get a compilation error because the java compiler doesn't know if
5:03
S…
Speaker 1 (12- Wildcards)
the object that we're trying to get is compatible with a user.
5:06
S…
Speaker 1 (12- Wildcards)
It could be a string object,
5:07
S…
Speaker 1 (12- Wildcards)
it could be a daytime object.
5:09
S…
Speaker 1 (12- Wildcards)
That is the reason why when we use the super keyword,
5:12
S…
Speaker 1 (12- Wildcards)
we can get an object from this list and store it in a variable of
5:16
S…
Speaker 1 (12- Wildcards)
type object.
5:17
S…
Speaker 1 (12- Wildcards)
So to recap,
5:19
S…
Speaker 1 (12- Wildcards)
if you want to read from this list,
5:20
S…
Speaker 1 (12- Wildcards)
you should use the extends keyword.
5:22
S…
Speaker 1 (12- Wildcards)
If you want to add to it,
5:23
S…
Speaker 1 (12- Wildcards)
you should use the super keyword.
This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy
Özet
Bu transkrip için bir AI özeti oluşturmak için Özetle' ye tıklayın.
Toplamlayarak...
Bu transkrip hakkında AI'ye sor
Bu transkrip hakkında herhangi bir şey sor — yapay zeka ilgili bölümleri bulur ve cevap verir.