Check out a free preview of the full Java Fundamentals course

The "Type Casting" 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 converting an object's type into another via type casting and demonstrates how to downcast a type. When casting types, the objects must be related in the inheritance hierarchy tree.


Transcript from the "Type Casting" Lesson

>> So when working with polymorphism, sometimes objects need to be what's called type casting. And type casting is the implicit or explicit act of converting an object type into a different type. So casting can be performed upward or downward on an inheritance tree. For example, let's say we have dog and cat classes that both inherit from the animal class here, when we create an object with the type of the superclass, but we instantiate it with the type of the subclass, this is an implicit up cast, right?

I've casted it upward from a dog to animal. In an implicit up cast, the object now only has access to the methods of the class that it was up casted to. For example, even though the object sasha is an instance of the dog class, its type is animal, so this object cannot reference members that are specific to the dog class, such as fetch.

So here, if we say sasha There is no fetch there. So in order to access methods from the subclass if you just want it to do this, then we need to explicitly downcast that object to a dog, the type dog. So we are going to downcast sasha, sasha the cat [LAUGH] to enable her to scratch her itch cuz right now sasha the cat cannot access the scratch method in the cat class.

All right, so sasha the cat, she was able to make a sound, and this is really interesting because when we called make sound, it executed the make sound method from the subclass, right? The one that was the overriding implementation. But other than that, it's not able to access any other methods that are in that class.

So it gets a little wonky in the brain. But let's say that we want sasha to scratch. We'll say sasha., and it's not explicitly here for her to scratch, but you say I know it exists, I say scratch. If you do this, notice you have an error here.

And it's saying we don't know what you're talking about. So in order to explicitly downcast sasha from animal to dog just so we can make this call, what we'll do is right before the object, we're going to put in parentheses, the class that we want to downcast to.

So that would be cat. So this alone, it's quickly changing sasha to a cat, but in order to make that whole thing an object, because we haven't stored that anywhere, we will put one more set of parentheses around the whole thing, making it an object, and then it can X's cat.

Now, this is not changing the overall object sasha. Sasha is still defined the as an animal here. It's just a temporary casting to access something and then being able to do so. All right, so let's run and make sure sasha can scratch our itch. Yes.
>> So with this typecasting does it only have to do with objects or is it also a term that's applicable to fields?

For example, let's say you're doing, I don't know, integer.2string to treat a number as a string, would that be considered typecasting as well, or is that just something different?
>> So that would be something different because you only can casts upward or downward within your own hierarchy. So integer and string have no hierarchy together.

So when you say two string is basically creating a brand new object and giving you that as a string. It's not doing a cast. If you try it and that's my tip to you, be very careful with typecasting, because if I try to cast this to anything else, let's say a rectangle for example, the compiler wouldn't necessarily complain about me doing that.

But once we get into execution mode and they actually try to do it, it's gonna fall apart. It's gonna throw an exception because I'm not able to cast into something that's not in my family tree.
>> So it doesn't know what you're talking about, or therefore it's probably passing a whole bunch of null values and therefore it doesn't work.

>> Well, it knows what I'm talking about, it knows that rectangle class, it just can't make these two be related when they're not. Yeah, okay. So again, that was a temporary casting just so that I can access that object. Matter of fact, if I did this, I say, sasha.scratch, and then I say sasha.scratch like this, again, doesn't know what I'm talking about, right?

So I did not change sasha's type at all. I only cast it for this one statement in order to do what I needed to do. If I wanted to make sasha permanently be something else, I want to cast it and then store it in an object, I can do that so that later I can access any of the methods from that subclass.

And so in order to do that, notice I've cast sasha to a dog on this second line, and then I've stored that into a new dog variable called sasha TheDog. And then if I wanted to access anything from the dog class, I could do so using sasha the dog.

>> What if you didn't create sasha but it was given to you and you want to try and cast it?
>> You can do that. So let's say you-.
>> What happens if you don't know what it is?
>> You always know what it is because if someone gave it to you that means you specify that in your parameter list, so you say the type or whatever.

>> But if they specify it as animal, they give you an animal, and you think it might be a cat, then you want it to say-.
>> Yeah, beautiful. Okay, I got some material on that. We'll get to that in a couple of minutes, all right? All right.

Yeah, so we talked about casting her. We talked about be careful, right? With the casting. Yeah, so be careful. This was a cat, right? So this is sasha as a cat and now I'm trying to cast sasha to a dog. Remember what I told you about the hierarchy tree?

You would think, animal, cat, dog, this is all in the same domain, we got the same mama. Look, they are not siblings, okay? They have no relationships. If sasha was a cat, I try to make sasha a dog. Again, there will not be a compilation error but when I run this, I will get an error an exception that says class cast exception.

So if you ever see that exception, that's what that means. It's gonna say I cannot make that cat, I can't cast it to a dog.
>> What do you do if you were to formalize instead of saying, a cat or the dog say, the cat is an animal like sasha the cat is sasha the animal or sasha the animals are sasha the dog.

>> Yeah, that's what we did in our code. So you could do that because they have a relationship, but you can't change it from a cat to a dog like that.
>> So, basically after like to navigate those relationships, step-by-step, if that's what you're trying to accomplish.
>> Yeah.

And you should only use casting if absolutely necessary. It's kinda dangerous [LAUGH] territory if you submit a pull request with some casting in it, anybody who's reviewing it it's a red flag, you look at it very carefully and try to think of other ways [LAUGH] to do this.

So yeah, be very careful if you use this.

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