Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Casting

 
Devendra Walanj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the mistake is at line 17



You should write



Now you can make the cast (Hector)x
 
Devendra Walanj
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicola Garofalo wrote:the mistake is at line 17



You should write



Now you can make the cast (Hector)x


Hey Nicola,
Thanks

I know that. But I want an explanation that why Casting with class Hector fails.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are trying to cast x, which is a Beta, to a Hector, which is a subclass of Beta. Now, if you had a Hector object you could cast it to a Beta because a Hector is-a Beta.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.





So what I was doing in

was that trying to downcast the generalised class Beta's reference.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic