

All right, so let's run and make sure sasha can scratch our itch. It's just a temporary casting to access something and then being able to do so. Sasha is still defined the as an animal here. Now, this is not changing the overall object sasha. 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. 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. And it's saying we don't know what you're talking about. If you do this, notice you have an error here. We'll say sasha., and it's not explicitly here for her to scratch, but you say I know it exists, I say scratch. But let's say that we want sasha to scratch. But other than that, it's not able to access any other methods that are in that 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. So we are going to downcast sasha, sasha the cat to enable her to scratch her itch cuz right now sasha the cat cannot access the scratch method in the cat class. 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 here, if we say sasha There is no fetch there. 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. In an implicit up cast, the object now only has access to the methods of the class that it was up casted to. I've casted it upward from a dog to animal. 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?

So casting can be performed upward or downward on an inheritance tree. And type casting is the implicit or explicit act of converting an object type into a different type. So when working with polymorphism, sometimes objects need to be what's called type casting.
