This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video we'll learn how to use type parameters with functions!
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In addition to classes, we can also
add type perimeters to methods.
0:00
Let's see how to do that by writing
our own add method in the main class.
0:04
It'll take an item in the box, and
0:09
inside we'll print out what type of item
it is and then add the item to the box.
0:12
Also, since the main function is static,
we'll need our function to be static too.
0:18
Let's start at the bottom of
the class with the static keyword.
0:23
Then, before we get to any parameters,
0:28
let's finish out the basic
method structure.
0:30
Let's use void for the return type,
name the method debugAdd,
0:33
add parenthesis and then the brackets.
0:39
Great.
0:43
Now we need to add our type parameter.
0:44
When adding a type
parameter to a function,
0:46
it needs to go to the left
of the return type.
0:49
So let's put our cursor right here and
add a new type parameter.
0:52
Let's stick with T for the name.
0:56
Then, for the object parameters,
we need an item and a box.
1:00
Let's require an object of type T,
called item, and
1:06
another which is a box,
for an object of type T.
1:11
So Box, then <T>.<T> and
1:15
we'll call this box box.
1:20
Perfect.
1:24
Inside the function let's start by
printing out the object's type.
1:25
So sout, and
1:30
then let's print the word type with
a colon after it and then a space.
1:33
Followed by the item's class name, so
+ item.getClass().getSimpleName().
1:40
To finish up our method, on the next
line let's add the item to the box,
1:49
box.add() and pass in the item.
1:55
Finally, we just need to replace our
add calls with calls to debugAdd.
1:59
Let's delete these two lines, and
2:04
then call debugAdd with our milk and
boxOfMilk,
2:08
and call it again with our oranges.
2:14
And boxOfOranges.
2:19
Last but not least,
let's form the app and see what happens.
2:21
Awesome.
2:29
Everything worked, and
we got our extra messages.
2:30
We're almost done with generics, but
2:33
before we go there's one more thing you
need to know about type parameters.
2:36
And that is, you can actually limit
what kinds of types are allowed.
2:41
Let's head over the box.Java and
take a look.
2:46
As is it now, we can create a box and
put any kind of object into it, but
2:51
another way, our T parameter will
definitely extend the object class.
2:56
And if you want to add limits
to your type parameter,
3:03
you can require the extend
of certain object.
3:06
For example,
if these boxes were reserved only for
3:09
food and we had a food class that the milk
and oranges classes inherited form,
3:12
we could require that our type
parameter extend from the food class.
3:17
We can also require our types to
implement interfaces, though for
3:22
whatever reason,
we still use the extends keyword.
3:26
So if we wanted to say that our type
parameter T needed to implement
3:29
an interface, we would say T extends,
and then the name of the interface.
3:32
Also, if you need your type
parameter to extend the class and
3:38
implement an interface or
implement multiple interfaces,
3:41
you would just add an ampersand and
then the next type.
3:45
Okay.
3:49
Let's take this back to just T and
wrap things up.
3:50
To put it all together, generics is just
a new word that means type parameters.
3:56
And type parameters are just a way to
pass types as parameters to methods and
4:01
classes.
4:06
So now, whenever you're creating a class
or a method, in addition to requiring
4:08
object parameters,
you can also require type parameters.
4:13
Without type parameters, we wouldn't be
able to make things like our box class.
4:17
And thanks to generics, not only can
we create a great box class, but
4:21
the Java team has already taken
our box idea and ran with it.
4:25
It's called
the Java Collections Framework.
4:29
And it gives us all kinds of different
ways to store groups of objects.
4:32
We'll save collections for
the next course, but for
4:36
now, let us know if you've
got any questions and
4:39
check out the teacher's notes below for
more information on generics.
4:42
Until next time.
4:46
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up