Java Fundamentals

instanceof Operator

Angie Jones

Angie Jones

Java Champion
Java Fundamentals

Check out a free preview of the full Java Fundamentals course

The "instanceof Operator" Lesson is part of the full, Java Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Angie discusses using the instanceof operator to check if an object is a specified class. A walkthrough of creating a method using instanceof is also covered in this segment.

Preview
Close

Transcript from the "instanceof Operator" Lesson

[00:00:00]
>> Back to the question of, well, if someone gave me an animal, how do I know that this is, like what's in its family, or if it was instantiated as a cat or a dog or something like that? There's this instanceof operator that exists in Java that allow you to check if something is an instance of a specific class.

[00:00:23]
So even if it's an animal, you can check to see if maybe it was instantiated with some other subclass. So here we have the Animal superclass and we have our two subclasses, the Dog and the Cat. So we can create an object named Sasha, has the type of Dog, I mean, has the type of Animal, and is an instance of Dog.

[00:00:50]
So if we wanted to check and see if Sasha is an instance of Animal, then this is how we would do that. Sasha, and then in between, we have the instanceof operator, and then we check for whatever class we wanna check for. This will return a Boolean of true or false.

[00:01:12]
So in this case, is sasha an instanceof Animal? Yes, she is. This one is, Checking if sasha is an instanceof Dog. This will also return true, because Sasha was instantiated as a dog.
>> Does that have any implications, like with logical operators, if you wanted to insert some kind of logic where you're saying, if this animal is a dog, do x-

[00:01:51]
>> Yeah.
>> If it is a cat, do y.
>> Yep, exactly. Cuz remember, like I said about the class cast exception and how dangerous that is and stuff like that, it's because you're making an assumption there, like, this must be probably an instance of such and such.

[00:02:09]
So I'm gonna downcast it to that so that I can do x, y, and z. And if it's not, then the whole program blows up. So you could do this first to say is it an instance of that? It is, great, now I feel safe down casting or whatever, all right?

[00:02:28]
If we wanted to check if sasha's an instanceof Cat, that would be false, right? Because there's nothing there that says Sasha is a cat, it's not in the hierarchy. All right, so let's practice with the instanceof operator. We are going to create a method that will put this in, I don't know what class, in our zoo class.

[00:02:58]
And this will feed any animal, but the type of food will vary based on the kind of animal it is. All right, so back to the zoo. Let's make a new method. We'll call this method feed. So public static void feed(), and let's make this one take an animal.

[00:03:31]
Okay, now, this is another great example of polymorphism in action. This feed says it takes an animal but that means it can take animal or any subclass of animal. So I've essentially set this at the highest level and that allows a bit of flexibility and how you call me, you can send that or any subclass of that, all right?

[00:04:07]
So let's say if the animal is an instanceof Dog, Then we'll just print out, here's your dog food. Else if animal is an instanceof Cat, we'll print, here's your cat food. All right, and then we'll go, and let's get Rocky fed. So we'll say rocky, wait, let's call it feed, feed(rocky), okay?

[00:05:02]
So Rocky is a dog, let me comment out all this other noise, all right. So Rocky is a dog, right, and we are sending a dog object into feed, again, we're able to do that because dog is a subclass of animal. So when we run this, let's make sure that Rocky is getting his dog food, yes.

[00:05:28]
And then let's do Animal sasha = new Dog(). What happens there, sasha, sorry, feed(sasha). So Sasha got the dog food, right, so it determined even though it's type is animal, yes it is an instanceof Dog. And then when we change Sasha from animal Sasha to a cat, Let's see what happens there.

[00:06:10]
Sasha got the cat food. Makes sense? Anybody lost? All right.
>> When you save a reference to a typecasted object, does that create a new object, or would changes to that object affect the original?
>> Yeah, so, on this line 18 here, this does not save this to a new object.

[00:06:43]
It's a temporary casting just to be able to access that scratch method. And then if you were to say sasha.scratch again, you would not be able to, because you don't have access. That type didn't stay beyond this one line, right? That type casting. If you wanted to store it into an object, you could do so yourself.

[00:07:13]
So you can say something like, Let me comment this. So I can say, all right, here's a newCat, And then do that. And then I can say newCat.scratch and that will work just fine. So I can store it, but automatically it is not.
>> Will changes to newCat effect Sasha?

[00:07:44]
So if we're going to use a setter on newCat for example, you know how you created a newCat casting?
>> Yeah, yeah, yeah.
>> If you set a property or change a property on newCat, is it going to affect?
>> Sasha? No, okay, good question. So if I would have changed something on newCat, newCat and Sasha are two different objects at this point.

[00:08:17]
So nothing I do with newCat affects Sasha, nothing I do on Sasha affects newCat.
>> Can we use var on the left side?
>> [LAUGH] You all like a good var. Okay, can we use var? Yes, we should be able to, let's see. Yeah, works just fine. That's dangerous territory though.

[00:08:41]
Using var in your typecast and stuff, you're asking for trouble.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now