Class Casting

Hello
By following code if I typecast foo's reference f with beta it goes and same with alpha too.
But when I type cast it with Hector it causes ClassCastException. I know about class casting but cannot get that through Hector class.

If you have trouble with the abstract idea of super classes and sub classes, try to think of it in real world terms. Like I have a Pet, which is a Dog, which is a GreatDane. You have a Pet, which is a Cat, which is a Siamese. Now, I can say you have a pet, or I can say you have a cat. You can say I have a pet, or a dog. I can't say you have a dog, even though it's a pet.

Now let's say that someone else has another kind of Pet, which is just general, no particular species. (The real world example sort of breaks down here, but try to follow along.) We can call it a pet, but we can't call it a dog or a cat, since those are specific kind of pets.

You are essentially doing that in your program. You are saying Hector is a Beta. (And I thought he was a Trojan!) However you are creating a general Beta, and trying to call it a Hector. That's just like calling that guy's Pet a Dog. Some Betas are Hectors, just as some Pets are Dogs, but not all of them.

Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12

posted Apr 21, 2010 16:05:27

0

Greg Charles wrote:If you have trouble with the abstract idea of super classes and sub classes, try to think of it in real world terms. Like I have a Pet, which is a Dog, which is a GreatDane. You have a Pet, which is a Cat, which is a Siamese. Now, I can say you have a pet, or I can say you have a cat. You can say I have a pet, or a dog. I can't say you have a dog, even though it's a pet.

Now let's say that someone else has another kind of Pet, which is just general, no particular species. (The real world example sort of breaks down here, but try to follow along.) We can call it a pet, but we can't call it a dog or a cat, since those are specific kind of pets.

You are essentially doing that in your program. You are saying Hector is a Beta. (And I thought he was a Trojan!) However you are creating a general Beta, and trying to call it a Hector. That's just like calling that guy's Pet a Dog. Some Betas are Hectors, just as some Pets are Dogs, but not all of them.

Keep in mind that casting doesn't do any kind of magic conversion - when you cast a value, you're only giving the compiler a hint like "here's a value, this is really a Hector object, you don't need to check it for me". If, at runtime, it turns out that the value is really not a Hector object, you'll get a ClassCastException.

So, you can't use a cast to magically convert a Dog into a Cat, or a Beta into a Hector.

Oh, I see. You were trying to force your general Beta to become a Hector. Some languages, like C++, do allow you to define behavior into a cast. So if you cast a general Pet object to a Dog, it would run code (that you had written) to tell the Pet to grow hair, four legs, and to smell bad when it got wet (and not that much better when it was dry). Java doesn't allow that though. You can write the conversion code, but you can't force it to run automatically via a cast.