1 00:00:00,270 --> 00:00:04,580 In addition to classes, we can also add type perimeters to methods. 2 00:00:04,580 --> 00:00:09,260 Let's see how to do that by writing our own add method in the main class. 3 00:00:09,260 --> 00:00:12,500 It'll take an item in the box, and 4 00:00:12,500 --> 00:00:17,250 inside we'll print out what type of item it is and then add the item to the box. 5 00:00:18,460 --> 00:00:22,800 Also, since the main function is static, we'll need our function to be static too. 6 00:00:23,870 --> 00:00:26,770 Let's start at the bottom of the class with the static keyword. 7 00:00:28,190 --> 00:00:30,980 Then, before we get to any parameters, 8 00:00:30,980 --> 00:00:33,720 let's finish out the basic method structure. 9 00:00:33,720 --> 00:00:38,290 Let's use void for the return type, name the method debugAdd, 10 00:00:39,390 --> 00:00:42,070 add parenthesis and then the brackets. 11 00:00:43,110 --> 00:00:44,130 Great. 12 00:00:44,130 --> 00:00:45,760 Now we need to add our type parameter. 13 00:00:46,840 --> 00:00:49,110 When adding a type parameter to a function, 14 00:00:49,110 --> 00:00:52,360 it needs to go to the left of the return type. 15 00:00:52,360 --> 00:00:56,990 So let's put our cursor right here and add a new type parameter. 16 00:00:56,990 --> 00:00:58,780 Let's stick with T for the name. 17 00:01:00,690 --> 00:01:05,710 Then, for the object parameters, we need an item and a box. 18 00:01:06,800 --> 00:01:11,610 Let's require an object of type T, called item, and 19 00:01:11,610 --> 00:01:15,146 another which is a box, for an object of type T. 20 00:01:15,146 --> 00:01:20,194 So Box, then . and 21 00:01:20,194 --> 00:01:24,860 we'll call this box box. 22 00:01:24,860 --> 00:01:25,910 Perfect. 23 00:01:25,910 --> 00:01:29,350 Inside the function let's start by printing out the object's type. 24 00:01:30,880 --> 00:01:33,640 So sout, and 25 00:01:33,640 --> 00:01:38,710 then let's print the word type with a colon after it and then a space. 26 00:01:40,040 --> 00:01:49,620 Followed by the item's class name, so + item.getClass().getSimpleName(). 27 00:01:49,620 --> 00:01:55,155 To finish up our method, on the next line let's add the item to the box, 28 00:01:55,155 --> 00:01:58,170 box.add() and pass in the item. 29 00:01:59,730 --> 00:02:04,820 Finally, we just need to replace our add calls with calls to debugAdd. 30 00:02:04,820 --> 00:02:08,893 Let's delete these two lines, and 31 00:02:08,893 --> 00:02:14,460 then call debugAdd with our milk and boxOfMilk, 32 00:02:14,460 --> 00:02:19,160 and call it again with our oranges. 33 00:02:19,160 --> 00:02:21,590 And boxOfOranges. 34 00:02:21,590 --> 00:02:24,800 Last but not least, let's form the app and see what happens. 35 00:02:29,430 --> 00:02:30,350 Awesome. 36 00:02:30,350 --> 00:02:33,920 Everything worked, and we got our extra messages. 37 00:02:33,920 --> 00:02:36,290 We're almost done with generics, but 38 00:02:36,290 --> 00:02:41,090 before we go there's one more thing you need to know about type parameters. 39 00:02:41,090 --> 00:02:46,390 And that is, you can actually limit what kinds of types are allowed. 40 00:02:46,390 --> 00:02:48,670 Let's head over the box.Java and take a look. 41 00:02:51,280 --> 00:02:56,750 As is it now, we can create a box and put any kind of object into it, but 42 00:02:56,750 --> 00:03:03,240 another way, our T parameter will definitely extend the object class. 43 00:03:03,240 --> 00:03:06,160 And if you want to add limits to your type parameter, 44 00:03:06,160 --> 00:03:09,330 you can require the extend of certain object. 45 00:03:09,330 --> 00:03:12,360 For example, if these boxes were reserved only for 46 00:03:12,360 --> 00:03:17,540 food and we had a food class that the milk and oranges classes inherited form, 47 00:03:17,540 --> 00:03:22,030 we could require that our type parameter extend from the food class. 48 00:03:22,030 --> 00:03:26,100 We can also require our types to implement interfaces, though for 49 00:03:26,100 --> 00:03:29,330 whatever reason, we still use the extends keyword. 50 00:03:29,330 --> 00:03:32,890 So if we wanted to say that our type parameter T needed to implement 51 00:03:32,890 --> 00:03:38,205 an interface, we would say T extends, and then the name of the interface. 52 00:03:38,205 --> 00:03:41,705 Also, if you need your type parameter to extend the class and 53 00:03:41,705 --> 00:03:45,185 implement an interface or implement multiple interfaces, 54 00:03:45,185 --> 00:03:47,685 you would just add an ampersand and then the next type. 55 00:03:49,615 --> 00:03:50,415 Okay. 56 00:03:50,415 --> 00:03:53,262 Let's take this back to just T and wrap things up. 57 00:03:56,112 --> 00:04:01,700 To put it all together, generics is just a new word that means type parameters. 58 00:04:01,700 --> 00:04:06,430 And type parameters are just a way to pass types as parameters to methods and 59 00:04:06,430 --> 00:04:07,060 classes. 60 00:04:08,070 --> 00:04:13,120 So now, whenever you're creating a class or a method, in addition to requiring 61 00:04:13,120 --> 00:04:17,260 object parameters, you can also require type parameters. 62 00:04:17,260 --> 00:04:21,490 Without type parameters, we wouldn't be able to make things like our box class. 63 00:04:21,490 --> 00:04:25,900 And thanks to generics, not only can we create a great box class, but 64 00:04:25,900 --> 00:04:29,830 the Java team has already taken our box idea and ran with it. 65 00:04:29,830 --> 00:04:32,180 It's called the Java Collections Framework. 66 00:04:32,180 --> 00:04:36,818 And it gives us all kinds of different ways to store groups of objects. 67 00:04:36,818 --> 00:04:39,618 We'll save collections for the next course, but for 68 00:04:39,618 --> 00:04:42,298 now, let us know if you've got any questions and 69 00:04:42,298 --> 00:04:46,268 check out the teacher's notes below for more information on generics. 70 00:04:46,268 --> 00:04:47,058 Until next time.