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 take a look at the kinds of problems we can run into without generics!
Milk.java
class Milk {
void drink() {
System.out.println("You drink the milk.");
}
}
Oranges.java
class Oranges {
void juggle() {
System.out.println("You drop the oranges on the ground.");
}
}
Box.java
class Box {
private Object contents;
void add(Object thing) {
if (contents == null) {
contents = thing;
} else {
System.out.println("The box is full.");
}
}
Object remove() {
if (contents == null) {
System.out.println("The box is empty.");
return null;
} else {
Object thing = contents;
contents = null;
return thing;
}
}
}
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
[MUSIC]
0:00
Hi, I'm Ben, and in this course,
we're going to learn about generics.
0:09
But before we get to any formal
definitions, I think it'll help if
0:14
we start by looking at an example of
the problem generics try to solve.
0:17
Let's start by creating a new project.
0:21
And let's click Next.
0:26
Select the template.
0:28
Hit Next again.
0:31
And let's name our project Generics.
0:32
And we can leave the package
as com.teamtreehouse.
0:35
And I'll full-screen my app
to make it easier to see.
0:40
Now that we've got an empty project,
0:43
we've got a few things to copy
in from the teacher's notes.
0:45
First, let's create a new class,
named Milk.
0:49
So right-click on the package,
new class, and I'll call it Milk.
0:55
And then let's paste in our Milk class
over the existing class Perfect.
1:00
Then let's do the same thing with
the Oranges and Box classes.
1:08
So we'll right click,
New > Java Class, Oranges, and
1:14
then we'll paste over it with
what's in the teacher's notes.
1:18
And then we'll do New > Java Class
one more time for the Box class.
1:27
And we'll paste that in.
1:37
Looking back at the Milk and
Oranges classes,
1:44
notice that Milk has
a function called drink.
1:47
And Oranges has a function called juggle.
1:51
And they each print something
out to the console.
1:54
Also, to get classes side-by-side like
this, you just right-click on the tab, and
1:57
choose Split Vertically.
2:02
All right,
that's enough about Milk and Oranges.
2:03
Let's get on to the Box class.
2:06
As you might have guessed,
this class aims to represent a box.
2:08
Though before we dig into the
implementation details, let's use Cmd or
2:12
Ctrl+Shift+- to start
with just the basics.
2:16
At the top, we have the contents of
the box, represented as an object.
2:20
Then we have an add method to
add an object to the box, and
2:26
a remove method to take an object out
of the box, which returns an object.
2:30
Diving into the add method,
we start by making sure the box is empty.
2:36
And if it is,
we update the contents variable.
2:43
Otherwise, we print out, The box is full.
2:47
Moving on to the remove function, again,
we start by checking if the box is empty.
2:50
And if it is, we tell the user that
the box is empty and return null.
2:56
If the box isn't empty,
then we pull out the contents of the box
3:01
into a new variable,
update the contents to be null, and
3:06
then return that new variable,
which is holding the contents.
3:11
So far, so good.
3:16
Let's head over to Main.java and
try it out.
3:17
Let's delete the comment, and
3:21
start by creating a new Milk object and
a new Oranges object.
3:23
So Milk milk = new Milk,
3:28
and Oranges oranges = new Oranges.
3:33
Then let's leave a space and create
a new Box variable to hold our milk.
3:41
So Box, and let's call it boxOfMilk.
3:46
And let's set it equal to a new Box.
3:49
And let's create another Box for
our oranges.
3:53
Box boxOfOranges = new Box.
3:56
Awesome, now that we've got our boxes,
4:02
let's use the add
function to fill them up.
4:05
Let's add the milk to the boxOfMilk.
4:07
So boxOfMilk.add, and
we'll pass in the milk.
4:11
And add the oranges to the boxOfOranges.
4:16
So boxOfOranges.add, and
pass in the oranges.
4:18
With our boxes filled, the only thing
left to do is take our objects back
4:25
out of the boxes and
call the respective functions.
4:30
Drink for the milk, and
juggle for the oranges.
4:33
Let's add another space, and
start with retrieving the milk.
4:36
Let's type boxOfMilk.remove to
get our milk back as an object.
4:40
Remember, the Box class stores
its contents as an object.
4:47
So if we want this to be a milk
object instead of just an object,
4:51
we'll need to add a cast.
4:55
Let's add the cast at the beginning
by putting it in parentheses.
4:58
Then we'll need to add parentheses
around the whole thing,
5:05
Before finishing up by calling
the drink method, so .drink.
5:11
Then let's do the same
thing with the oranges,
5:16
except this time we'll
call the juggle method.
5:19
Let's add two left parentheses,
and then Oranges for the cast.
5:21
Move over one parenthesis and
type boxOfOranges.remove.
5:28
And finally, add the call to juggle.
5:33
To finish up, let's run the code and
make sure everything works.
5:37
Perfect, but while this code does work,
and there's not necessarily anything wrong
5:43
with it, there are a couple situations
that can get us into trouble.
5:48
For example,
what happens if we switch the boxes?
5:52
Let's put the oranges in the boxOfMilk,
And
5:56
the milk in the boxOfOranges.
6:02
Looks good so far, now let's run the code.
6:05
And of course, we get an error.
6:10
Turns out we can't cast an Oranges
object into a Milk object.
6:13
Wouldn't it be great if it would have
just stopped us from putting the oranges
6:17
into the boxOfMilk or
the milk into the boxOfOranges?
6:20
In the next video, we'll see how
to do that by using generics.
6:24
But first, let's go over here and
6:28
click on Indent with 4 spaces
to get rid of that warning.
6:31
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