aspose file tools*
The moose likes Java in General and the fly likes Class Casting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Class Casting" Watch "Class Casting" New topic
Author

Class Casting

Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
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.



Keep Moving Forward
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
the mistake is at line 17



You should write



Now you can make the cast (Hector)x


Bye,
Nicola
Devendra Walanj
Greenhorn

Joined: Apr 21, 2010
Posts: 12
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

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
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

Joined: Aug 16, 2005
Posts: 14164
    
  19

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class Casting