Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Casting Instances4:37 with Ben Deitch
In this video we'll learn how to use type casting (or casts) to move an Object up (or down) it's family tree. We'll also see how to use the 'instanceof' operator to determine if an Object is of a specific type.
We just saw how we can group together different objects 0:00 by using an array with a common base class. 0:03 Now let's see how to use a cast so we can call makeSound on our dog object. 0:06 First, since we got rid of our dog variable, let's make a new one. 0:11 Let's add a new line after our object array and type dog. 0:16 And we'll name it dog, and set it equal to the first item in our array. 0:20 So list at position 0, which gives us an incompatible types error. 0:25 This is where the cast comes in. 0:33 A cast, or type casting, is when you tell Java that an object 0:36 is a more specific descendant of that object. 0:40 Essentially moving the object up its family tree. 0:44 For example, if we were storing a car as a vehicle object and 0:48 needed to turn it back to a car object, we would use a cast. 0:53 To use a cast, you just write the name of the class between parentheses. 0:57 So right after the equal sign, 1:02 let's add a cast to change list 0 to a dog object. 1:05 So parenthesis and inside them we write the class, Dog. 1:10 Nice, it doesn't look like we have any errors. 1:15 And if we run it, We get the right output. 1:19 But before we move on, 1:26 let's see what happens if we try to merge these two lines. 1:27 Let's hide the run panel, and then delete line seven, 1:31 and change dog to list at position zero. 1:39 Then let's add the cast, and it doesn't work. 1:45 It turns out that the call to makeSound happens before the cast. 1:50 So to make sure that the cast happens first, 1:56 we just need to use some parentheses. 2:02 Okay, we've got an array of objects and 2:08 seen how we can use casts to move an object up its family tree. 2:11 But this only works because we know exactly where our dog is in the array. 2:15 If we accidentally tried to cast our dog food object to a dog, 2:21 we'd crash the program. 2:25 And just to make sure, Yep, we get a class cast exception. 2:27 Cmd or Ctrl+Z to undo, and we're back to working code. 2:38 So instead of just picking out the dog object, 2:43 let's loop through our list array and only call makeSound if the object 2:46 actually has a makeSound method, meaning the object is an animal. 2:51 Let's delete line 7 again, and let's use a for each loop to loop through our array. 2:56 For object, we'll call it object, and, 3:04 which is represented by a colon, 3:09 our list array, and add the brackets. 3:13 And inside the for loop, we need to determine if this object is an animal. 3:17 Luckily, there's a keyword for this. 3:22 Let's write an if statement. 3:24 And for the condition, let's check that object, instance of. 3:28 Animal. 3:36 And notice that instanceof is all one word and all lowercase. 3:37 Using the instanceof operator allows us to check if one object 3:44 is an instance of another. 3:49 So inside the if statement, 3:51 after we add our brackets, object is guaranteed to be an animal. 3:54 However, even though it's guaranteed to be an animal, we'll still need to cast it. 4:00 Let's type object., and choose makeSound 4:06 to have IntelliJ automatically add the cast and call the method. 4:10 Awesome, now let's run the app. 4:16 And perfect, we're still getting the same output. 4:21 Using casts and the instance of operator 4:25 gives us a lot more freedom with how we handle our objects. 4:28 Why don't you get a little more practice, and 4:31 in the next video, we'll take a deeper look at the object class. 4:34
You need to sign up for Treehouse in order to download course files.Sign up