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