9- Generic Methods
Jun 20, 2026 06:32
· 4:48
· English
· Whisper Turbo
· 2 speakers
இந்த நகல் காலாவதியாகும் 24 நாட்கள்.
நிலையான சேமிப்புக்காக மேம்படுத்தல் →
காட்டு
0:03
S…
Speaker 2 (9- Generic Methods)
So you have learned how to create generic classes.
0:05
S…
Speaker 2 (9- Generic Methods)
Now in this generic class we have a couple of generic methods add
0:09
S…
Speaker 2 (9- Generic Methods)
and get,
0:10
S…
Speaker 2 (9- Generic Methods)
but we can also declare a generic method inside a non generic
0:14
S…
Speaker 1 (9- Generic Methods)
class.
0:15
S…
Speaker 1 (9- Generic Methods)
Let me show you.
0:16
S…
Speaker 2 (9- Generic Methods)
So let's add a new class to this package.
0:19
S…
Speaker 1 (9- Generic Methods)
We're going to call it utils.
0:21
S…
Speaker 2 (9- Generic Methods)
This class is going to have a bunch of utility methods.
0:26
S…
Speaker 2 (9- Generic Methods)
Now, let's say we want to get the maximum of two values.
0:29
S…
Speaker 2 (9- Generic Methods)
So here we can add a public method.
0:31
S…
Speaker 2 (9- Generic Methods)
We make it static so we don't have to create an instance of the utils class.
0:35
S…
Speaker 2 (9- Generic Methods)
This is a common convention for declaring utility methods.
0:39
S…
Speaker 2 (9- Generic Methods)
We make them static.
0:40
S…
Speaker 2 (9- Generic Methods)
Now we return an integer,
0:41
S…
Speaker 2 (9- Generic Methods)
we call the method max and give it two parameters,
0:44
S…
Speaker 2 (9- Generic Methods)
first and second.
0:46
S…
Speaker 2 (9- Generic Methods)
Here we can use the ternary operator to return the
0:50
S…
Speaker 2 (9- Generic Methods)
maximum of these two values.
0:52
S…
Speaker 1 (9- Generic Methods)
So we return,
0:53
S…
Speaker 2 (9- Generic Methods)
here we have a boolean expression.
0:56
S…
Speaker 2 (9- Generic Methods)
if first is greater than the second,
0:58
S…
Speaker 1 (9- Generic Methods)
return first,
1:01
S…
Speaker 2 (9- Generic Methods)
otherwise return second.
1:02
S…
Speaker 1 (9- Generic Methods)
Pretty simple,
1:04
S…
Speaker 1 (9- Generic Methods)
right? Now,
1:05
S…
Speaker 2 (9- Generic Methods)
we want to make this method generic.
1:06
S…
Speaker 2 (9- Generic Methods)
So instead of getting two integers and returning an integer,
1:09
S…
Speaker 2 (9- Generic Methods)
you want to be able to work with different types of objects.
1:12
S…
Speaker 2 (9- Generic Methods)
Now we don't necessarily have to make this class generic,
1:15
S…
Speaker 2 (9- Generic Methods)
we can declare a generic method inside a non -generic class.
1:19
S…
Speaker 1 (9- Generic Methods)
Let me show you.
1:20
S…
Speaker 1 (9- Generic Methods)
So,
1:21
S…
Speaker 2 (9- Generic Methods)
just before the return type,
1:24
S…
Speaker 2 (9- Generic Methods)
we add the generic type parameter.
1:26
S…
Speaker 2 (9- Generic Methods)
Okay, now we replace all these integers with T.
1:30
S…
Speaker 2 (9- Generic Methods)
So here's the return type and the two parameters.
1:34
S…
Speaker 1 (9- Generic Methods)
We have a compilation error because
1:38
S…
Speaker 2 (9- Generic Methods)
we cannot apply the comparison operator between objects of type T.
1:42
S…
Speaker 2 (9- Generic Methods)
We can only apply it to numbers and characters.
1:46
S…
Speaker 1 (9- Generic Methods)
So how can we compare these two objects?
1:48
S…
Speaker 1 (9- Generic Methods)
Well,
1:49
S…
Speaker 2 (9- Generic Methods)
you learned about the comparable interface in the last video.
1:52
S…
Speaker 2 (9- Generic Methods)
So here we can tell the Java compiler that this T is an
1:56
S…
Speaker 2 (9- Generic Methods)
object that implements the comparable interface.
1:59
S…
Speaker 1 (9- Generic Methods)
How can we do that?
2:01
S…
Speaker 1 (9- Generic Methods)
We type extends comparable of T.
2:05
S…
Speaker 1 (9- Generic Methods)
So we are applying a constraint.
2:07
S…
Speaker 1 (9- Generic Methods)
Now,
2:08
S…
Speaker 1 (9- Generic Methods)
when we type first dot,
2:12
S…
Speaker 2 (9- Generic Methods)
we see that this object has a method called compare to,
2:16
S…
Speaker 2 (9- Generic Methods)
which takes an object of type T.
2:18
S…
Speaker 2 (9- Generic Methods)
So we can compare first with second.
2:21
S…
Speaker 2 (9- Generic Methods)
If it's less than zero,
2:23
S…
Speaker 1 (9- Generic Methods)
then we're going to return second,
2:27
S…
Speaker 2 (9- Generic Methods)
otherwise we're going to return first.
2:29
S…
Speaker 2 (9- Generic Methods)
So this is one application of the compareTo method.
2:33
S…
Speaker 1 (9- Generic Methods)
Now let's test this method.
2:35
S…
Speaker 2 (9- Generic Methods)
So, back to the main class,
2:37
S…
Speaker 1 (9- Generic Methods)
here we type utils dot,
2:40
S…
Speaker 2 (9- Generic Methods)
we call the max method.
2:42
S…
Speaker 1 (9- Generic Methods)
Now look at the type of this parameter,
2:44
S…
Speaker 1 (9- Generic Methods)
it's t,
2:45
S…
Speaker 2 (9- Generic Methods)
because at this point the Java compiler doesn't know if
2:49
S…
Speaker 2 (9- Generic Methods)
we're going to pass an integer,
2:50
S…
Speaker 1 (9- Generic Methods)
a character,
2:51
S…
Speaker 1 (9- Generic Methods)
a string,
2:52
S…
Speaker 2 (9- Generic Methods)
a user object,
2:52
S…
Speaker 1 (9- Generic Methods)
or what.
2:53
S…
Speaker 2 (9- Generic Methods)
Now, the moment I pass a value here,
2:56
S…
Speaker 1 (9- Generic Methods)
let's say 1,
2:57
S…
Speaker 2 (9- Generic Methods)
the Java compiler knows this is an integer,
3:00
S…
Speaker 2 (9- Generic Methods)
so it wraps it inside the integer class,
3:03
S…
Speaker 2 (9- Generic Methods)
and it knows that the second parameter should also be an integer.
3:06
S…
Speaker 2 (9- Generic Methods)
So I cannot pass a character or a string here.
3:09
S…
Speaker 1 (9- Generic Methods)
This is the benefit of using generics.
3:11
S…
Speaker 2 (9- Generic Methods)
Instead of adding two parameters of type object,
3:14
S…
Speaker 2 (9- Generic Methods)
we use a generic method.
3:16
S…
Speaker 1 (9- Generic Methods)
Okay?
3:17
S…
Speaker 1 (9- Generic Methods)
Now,
3:18
S…
Speaker 1 (9- Generic Methods)
back in the main class.
3:20
S…
Speaker 2 (9- Generic Methods)
So let's pass 3,
3:22
S…
Speaker 2 (9- Generic Methods)
this will return the max we store it here and then print it
3:26
S…
Speaker 2 (9- Generic Methods)
on the console.
3:27
S…
Speaker 1 (9- Generic Methods)
So the max is 3.
3:29
S…
Speaker 2 (9- Generic Methods)
Now we can also pass two user objects,
3:32
S…
Speaker 2 (9- Generic Methods)
so let's say one user with 10 points and another
3:37
S…
Speaker 2 (9- Generic Methods)
user with 20 points.
3:39
S…
Speaker 2 (9- Generic Methods)
Obviously the return type of this method is now going to be a user object.
3:43
S…
Speaker 2 (9- Generic Methods)
So let me print it
3:47
S…
Speaker 2 (9- Generic Methods)
we get this long string which includes the fully qualified name of the class
3:52
S…
Speaker 2 (9- Generic Methods)
followed by an add sign and then a hash code.
3:55
S…
Speaker 2 (9- Generic Methods)
I talked about this in the previous parts of this series.
3:57
S…
Speaker 1 (9- Generic Methods)
So when we pass an object to the print line method,
4:00
S…
Speaker 2 (9- Generic Methods)
the tool string method on that object gets called.
4:03
S…
Speaker 2 (9- Generic Methods)
So that long string that you saw returns from the default implementation
4:07
S…
Speaker 2 (9- Generic Methods)
of the tool string method.
4:08
S…
Speaker 2 (9- Generic Methods)
To solve this problem,
4:10
S…
Speaker 2 (9- Generic Methods)
we go back to the user class and overwrite the tool string method.
4:14
S…
Speaker 2 (9- Generic Methods)
So first we apply
4:16
S…
Speaker 2 (9- Generic Methods)
the overwrite annotation.
4:18
S…
Speaker 2 (9- Generic Methods)
We're telling the Java compiler that we're overwriting one of the methods inherited
4:23
S…
Speaker 2 (9- Generic Methods)
from the base class.
4:24
S…
Speaker 2 (9- Generic Methods)
And then we type public string to string.
4:28
S…
Speaker 1 (9- Generic Methods)
And here we can return a simple string
4:32
S…
Speaker 1 (9- Generic Methods)
like this.
4:33
S…
Speaker 2 (9- Generic Methods)
Points equals plus points.
4:36
S…
Speaker 1 (9- Generic Methods)
Now,
4:37
S…
Speaker 1 (9- Generic Methods)
let's run a program so you can see that the user
4:42
S…
Speaker 2 (9- Generic Methods)
with 20 points is the maximum of these two user objects.
This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy
சுருக்கம்
இந்த நகலெடுப்புக்கான AI சுருக்கத்தை உருவாக்க சுருக்கம் என்பதை க்ளிக் செய்யவும்.
சுருக்கப்படுகிறது...
இந்த நகலை AI க்குக் கேட்கவும்
இந்த நூலைப் பற்றி ஏதாவது கேட்கவும் - AI தொடர்புடைய பகுதிகளைக் கண்டுபிடித்து பதிலளிக்கும்.