1 00:00:00,360 --> 00:00:03,570 We just saw how we can group together different objects 2 00:00:03,570 --> 00:00:06,690 by using an array with a common base class. 3 00:00:06,690 --> 00:00:11,650 Now let's see how to use a cast so we can call makeSound on our dog object. 4 00:00:11,650 --> 00:00:15,250 First, since we got rid of our dog variable, let's make a new one. 5 00:00:16,410 --> 00:00:20,921 Let's add a new line after our object array and type dog. 6 00:00:20,921 --> 00:00:25,993 And we'll name it dog, and set it equal to the first item in our array. 7 00:00:25,993 --> 00:00:33,301 So list at position 0, which gives us an incompatible types error. 8 00:00:33,301 --> 00:00:36,015 This is where the cast comes in. 9 00:00:36,015 --> 00:00:40,664 A cast, or type casting, is when you tell Java that an object 10 00:00:40,664 --> 00:00:44,950 is a more specific descendant of that object. 11 00:00:44,950 --> 00:00:48,650 Essentially moving the object up its family tree. 12 00:00:48,650 --> 00:00:53,380 For example, if we were storing a car as a vehicle object and 13 00:00:53,380 --> 00:00:57,810 needed to turn it back to a car object, we would use a cast. 14 00:00:57,810 --> 00:01:02,660 To use a cast, you just write the name of the class between parentheses. 15 00:01:02,660 --> 00:01:05,654 So right after the equal sign, 16 00:01:05,654 --> 00:01:10,500 let's add a cast to change list 0 to a dog object. 17 00:01:10,500 --> 00:01:15,297 So parenthesis and inside them we write the class, Dog. 18 00:01:15,297 --> 00:01:19,790 Nice, it doesn't look like we have any errors. 19 00:01:19,790 --> 00:01:26,350 And if we run it, We get the right output. 20 00:01:26,350 --> 00:01:27,870 But before we move on, 21 00:01:27,870 --> 00:01:31,660 let's see what happens if we try to merge these two lines. 22 00:01:31,660 --> 00:01:36,400 Let's hide the run panel, and then delete line seven, 23 00:01:39,490 --> 00:01:45,580 and change dog to list at position zero. 24 00:01:45,580 --> 00:01:49,710 Then let's add the cast, and it doesn't work. 25 00:01:50,810 --> 00:01:55,380 It turns out that the call to makeSound happens before the cast. 26 00:01:56,970 --> 00:02:02,974 So to make sure that the cast happens first, 27 00:02:02,974 --> 00:02:08,179 we just need to use some parentheses. 28 00:02:08,179 --> 00:02:11,171 Okay, we've got an array of objects and 29 00:02:11,171 --> 00:02:15,670 seen how we can use casts to move an object up its family tree. 30 00:02:15,670 --> 00:02:21,334 But this only works because we know exactly where our dog is in the array. 31 00:02:21,334 --> 00:02:25,734 If we accidentally tried to cast our dog food object to a dog, 32 00:02:25,734 --> 00:02:27,644 we'd crash the program. 33 00:02:27,644 --> 00:02:34,440 And just to make sure, Yep, we get a class cast exception. 34 00:02:38,485 --> 00:02:43,174 Cmd or Ctrl+Z to undo, and we're back to working code. 35 00:02:43,174 --> 00:02:46,577 So instead of just picking out the dog object, 36 00:02:46,577 --> 00:02:51,814 let's loop through our list array and only call makeSound if the object 37 00:02:51,814 --> 00:02:56,808 actually has a makeSound method, meaning the object is an animal. 38 00:02:56,808 --> 00:03:04,290 Let's delete line 7 again, and let's use a for each loop to loop through our array. 39 00:03:04,290 --> 00:03:09,258 For object, we'll call it object, and, 40 00:03:09,258 --> 00:03:13,122 which is represented by a colon, 41 00:03:13,122 --> 00:03:17,679 our list array, and add the brackets. 42 00:03:17,679 --> 00:03:22,274 And inside the for loop, we need to determine if this object is an animal. 43 00:03:22,274 --> 00:03:24,698 Luckily, there's a keyword for this. 44 00:03:24,698 --> 00:03:25,950 Let's write an if statement. 45 00:03:28,097 --> 00:03:33,159 And for the condition, let's check that object, instance of. 46 00:03:36,876 --> 00:03:37,442 Animal. 47 00:03:37,442 --> 00:03:44,990 And notice that instanceof is all one word and all lowercase. 48 00:03:44,990 --> 00:03:49,290 Using the instanceof operator allows us to check if one object 49 00:03:49,290 --> 00:03:50,720 is an instance of another. 50 00:03:51,910 --> 00:03:54,140 So inside the if statement, 51 00:03:54,140 --> 00:03:59,170 after we add our brackets, object is guaranteed to be an animal. 52 00:04:00,600 --> 00:04:06,060 However, even though it's guaranteed to be an animal, we'll still need to cast it. 53 00:04:06,060 --> 00:04:10,560 Let's type object., and choose makeSound 54 00:04:10,560 --> 00:04:14,520 to have IntelliJ automatically add the cast and call the method. 55 00:04:16,030 --> 00:04:17,757 Awesome, now let's run the app. 56 00:04:21,276 --> 00:04:23,990 And perfect, we're still getting the same output. 57 00:04:25,360 --> 00:04:28,130 Using casts and the instance of operator 58 00:04:28,130 --> 00:04:31,930 gives us a lot more freedom with how we handle our objects. 59 00:04:31,930 --> 00:04:34,120 Why don't you get a little more practice, and 60 00:04:34,120 --> 00:04:37,660 in the next video, we'll take a deeper look at the object class.