1 00:00:00,003 --> 00:00:09,085 [MUSIC] 2 00:00:09,085 --> 00:00:14,040 Hi, I'm Ben, and in this course, we're going to learn about generics. 3 00:00:14,040 --> 00:00:17,260 But before we get to any formal definitions, I think it'll help if 4 00:00:17,260 --> 00:00:21,550 we start by looking at an example of the problem generics try to solve. 5 00:00:21,550 --> 00:00:23,866 Let's start by creating a new project. 6 00:00:26,192 --> 00:00:27,410 And let's click Next. 7 00:00:28,840 --> 00:00:29,720 Select the template. 8 00:00:31,090 --> 00:00:31,820 Hit Next again. 9 00:00:32,900 --> 00:00:34,650 And let's name our project Generics. 10 00:00:35,770 --> 00:00:38,735 And we can leave the package as com.teamtreehouse. 11 00:00:40,702 --> 00:00:43,930 And I'll full-screen my app to make it easier to see. 12 00:00:43,930 --> 00:00:45,636 Now that we've got an empty project, 13 00:00:45,636 --> 00:00:49,150 we've got a few things to copy in from the teacher's notes. 14 00:00:49,150 --> 00:00:52,366 First, let's create a new class, named Milk. 15 00:00:55,491 --> 00:01:00,300 So right-click on the package, new class, and I'll call it Milk. 16 00:01:00,300 --> 00:01:08,120 And then let's paste in our Milk class over the existing class Perfect. 17 00:01:08,120 --> 00:01:11,819 Then let's do the same thing with the Oranges and Box classes. 18 00:01:14,385 --> 00:01:18,667 So we'll right click, New > Java Class, Oranges, and 19 00:01:18,667 --> 00:01:23,503 then we'll paste over it with what's in the teacher's notes. 20 00:01:27,948 --> 00:01:33,748 And then we'll do New > Java Class one more time for the Box class. 21 00:01:37,559 --> 00:01:38,791 And we'll paste that in. 22 00:01:44,181 --> 00:01:47,097 Looking back at the Milk and Oranges classes, 23 00:01:47,097 --> 00:01:51,000 notice that Milk has a function called drink. 24 00:01:51,000 --> 00:01:54,040 And Oranges has a function called juggle. 25 00:01:54,040 --> 00:01:56,300 And they each print something out to the console. 26 00:01:57,320 --> 00:02:02,130 Also, to get classes side-by-side like this, you just right-click on the tab, and 27 00:02:02,130 --> 00:02:03,950 choose Split Vertically. 28 00:02:03,950 --> 00:02:06,410 All right, that's enough about Milk and Oranges. 29 00:02:06,410 --> 00:02:08,710 Let's get on to the Box class. 30 00:02:08,710 --> 00:02:12,810 As you might have guessed, this class aims to represent a box. 31 00:02:12,810 --> 00:02:16,762 Though before we dig into the implementation details, let's use Cmd or 32 00:02:16,762 --> 00:02:20,570 Ctrl+Shift+- to start with just the basics. 33 00:02:20,570 --> 00:02:25,150 At the top, we have the contents of the box, represented as an object. 34 00:02:26,570 --> 00:02:30,920 Then we have an add method to add an object to the box, and 35 00:02:30,920 --> 00:02:35,500 a remove method to take an object out of the box, which returns an object. 36 00:02:36,620 --> 00:02:41,830 Diving into the add method, we start by making sure the box is empty. 37 00:02:43,460 --> 00:02:46,130 And if it is, we update the contents variable. 38 00:02:47,150 --> 00:02:50,450 Otherwise, we print out, The box is full. 39 00:02:50,450 --> 00:02:56,450 Moving on to the remove function, again, we start by checking if the box is empty. 40 00:02:56,450 --> 00:03:01,870 And if it is, we tell the user that the box is empty and return null. 41 00:03:01,870 --> 00:03:06,825 If the box isn't empty, then we pull out the contents of the box 42 00:03:06,825 --> 00:03:11,960 into a new variable, update the contents to be null, and 43 00:03:11,960 --> 00:03:16,120 then return that new variable, which is holding the contents. 44 00:03:16,120 --> 00:03:17,370 So far, so good. 45 00:03:17,370 --> 00:03:19,720 Let's head over to Main.java and try it out. 46 00:03:21,370 --> 00:03:23,875 Let's delete the comment, and 47 00:03:23,875 --> 00:03:28,618 start by creating a new Milk object and a new Oranges object. 48 00:03:28,618 --> 00:03:33,892 So Milk milk = new Milk, 49 00:03:33,892 --> 00:03:41,690 and Oranges oranges = new Oranges. 50 00:03:41,690 --> 00:03:45,260 Then let's leave a space and create a new Box variable to hold our milk. 51 00:03:46,260 --> 00:03:48,720 So Box, and let's call it boxOfMilk. 52 00:03:49,950 --> 00:03:53,281 And let's set it equal to a new Box. 53 00:03:53,281 --> 00:03:56,153 And let's create another Box for our oranges. 54 00:03:56,153 --> 00:04:02,410 Box boxOfOranges = new Box. 55 00:04:02,410 --> 00:04:05,060 Awesome, now that we've got our boxes, 56 00:04:05,060 --> 00:04:07,840 let's use the add function to fill them up. 57 00:04:07,840 --> 00:04:09,875 Let's add the milk to the boxOfMilk. 58 00:04:11,100 --> 00:04:14,360 So boxOfMilk.add, and we'll pass in the milk. 59 00:04:16,130 --> 00:04:18,980 And add the oranges to the boxOfOranges. 60 00:04:18,980 --> 00:04:23,890 So boxOfOranges.add, and pass in the oranges. 61 00:04:25,600 --> 00:04:30,030 With our boxes filled, the only thing left to do is take our objects back 62 00:04:30,030 --> 00:04:33,430 out of the boxes and call the respective functions. 63 00:04:33,430 --> 00:04:36,750 Drink for the milk, and juggle for the oranges. 64 00:04:36,750 --> 00:04:40,372 Let's add another space, and start with retrieving the milk. 65 00:04:40,372 --> 00:04:47,040 Let's type boxOfMilk.remove to get our milk back as an object. 66 00:04:47,040 --> 00:04:51,330 Remember, the Box class stores its contents as an object. 67 00:04:51,330 --> 00:04:55,770 So if we want this to be a milk object instead of just an object, 68 00:04:55,770 --> 00:04:57,030 we'll need to add a cast. 69 00:04:58,330 --> 00:05:03,686 Let's add the cast at the beginning by putting it in parentheses. 70 00:05:05,601 --> 00:05:08,435 Then we'll need to add parentheses around the whole thing, 71 00:05:11,685 --> 00:05:16,720 Before finishing up by calling the drink method, so .drink. 72 00:05:16,720 --> 00:05:19,160 Then let's do the same thing with the oranges, 73 00:05:19,160 --> 00:05:20,960 except this time we'll call the juggle method. 74 00:05:21,990 --> 00:05:28,010 Let's add two left parentheses, and then Oranges for the cast. 75 00:05:28,010 --> 00:05:33,530 Move over one parenthesis and type boxOfOranges.remove. 76 00:05:33,530 --> 00:05:35,460 And finally, add the call to juggle. 77 00:05:37,160 --> 00:05:41,129 To finish up, let's run the code and make sure everything works. 78 00:05:43,433 --> 00:05:48,229 Perfect, but while this code does work, and there's not necessarily anything wrong 79 00:05:48,229 --> 00:05:52,880 with it, there are a couple situations that can get us into trouble. 80 00:05:52,880 --> 00:05:56,770 For example, what happens if we switch the boxes? 81 00:05:56,770 --> 00:06:02,077 Let's put the oranges in the boxOfMilk, And 82 00:06:02,077 --> 00:06:05,500 the milk in the boxOfOranges. 83 00:06:05,500 --> 00:06:08,389 Looks good so far, now let's run the code. 84 00:06:10,298 --> 00:06:13,120 And of course, we get an error. 85 00:06:13,120 --> 00:06:17,290 Turns out we can't cast an Oranges object into a Milk object. 86 00:06:17,290 --> 00:06:20,920 Wouldn't it be great if it would have just stopped us from putting the oranges 87 00:06:20,920 --> 00:06:24,890 into the boxOfMilk or the milk into the boxOfOranges? 88 00:06:24,890 --> 00:06:28,880 In the next video, we'll see how to do that by using generics. 89 00:06:28,880 --> 00:06:31,230 But first, let's go over here and 90 00:06:31,230 --> 00:06:33,910 click on Indent with 4 spaces to get rid of that warning.