aspose file tools*
The moose likes Java in General and the fly likes ClassCastException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "ClassCastException" Watch "ClassCastException" New topic
Author

ClassCastException

Irina Irina
Greenhorn

Joined: May 29, 2004
Posts: 1
Hi All,

I have a problem with some code I just canged.

Previous version:

Interface ServiceRequest is implemented by concrete class OrderRequest.

When I cast a ServiceRequest object to an OrderRequest.....I get no exceptions.


Present case:

Interface ServiceRequest is extended by Interface UnparsedRequest and UnparsedRequest interface is implemented in another implementation class.

When I try to casr a ServiceRequest object to an UnparsedRequest...I get a ClassCastException error.

Do I get the ClassCastException because UnparsedReuest does not implement the ServiceRequest interface? I do not really want to case the ServiceRequest to the implementation of UnparsedRequest, because I do not want to restrict my "request" to a specilized implementation. At this point...I only need a general "UnparsedRequest".

Does anybody have any suggestion as to why I get this exception?
ThANKS
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Irina,

Welcome to JavaRanch!

Based on what you've said, everything should work fine; so you may have not properly implemented what you've described. Can you post some code -- the least possible amount of code that reproduces the problem? That would probably help a lot.


[Jess in Action][AskingGoodQuestions]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923


Interface ServiceRequest is extended by Interface UnparsedRequest...

When I try to cast a ServiceRequest object to an UnparsedRequest...I get a ClassCastException error.


ServiceRequest <|-- UnparsedRequest
ur = (UnparsedRequest) ServiceRequest;

You may cast a spezialized thing to a more general thing - not vice versa.
You may cast a Fish into an Animal, but not an Animal into an Fish, - perhaps the animal is a Cow.


http://home.arcor.de/hirnstrom/bewerbung
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
[IDLE CURIOSITY]
Suppose I have Bird, and AntiTankGrenade, which both have all the same member variables, but different methods. One is clearly not the subclass of the other, so a typecast between them will fail. But the logistics of the change seem perfectly doable. Why does this not work, and more generally, how does typecasting objects work?
[/IDLE CURIOSITY]


I've heard it takes forever to grow a woman from the ground
David Hibbs
Ranch Hand

Joined: Dec 19, 2002
Posts: 374
Originally posted by Joseph George:
[IDLE CURIOSITY]
Suppose I have Bird, and AntiTankGrenade, which both have all the same member variables, but different methods. One is clearly not the subclass of the other, so a typecast between them will fail. But the logistics of the change seem perfectly doable. Why does this not work, and more generally, how does typecasting objects work?
[/IDLE CURIOSITY]


Casting is simply provided as a way of saying "Treat this instance as a member of this class." This is important in that it allows you to access specific behaviors of the class.

Casting with a widening scope--i.e. from more specific to less specific--is implicit and always works in Java. You can assign an instance of any class to a variable declared to be of type Object, for example. Take the following code:

The code will compile and run. This is important as it allows highly dynamic behavior via polymorphism and it also allows generic containers, i.e. the collections API.

Now say that I want to get a substring starting at the 3rd character.

will not compile because Object does not have a substring method! However, if I use a typecast...

The code will now compile, linking to the correct substring method.

As far as your comment goes on the logistics seeming doable, yes--it probably could be done. However, Java is a "strongly typed" language. Hence, such a conversion is not allowed by the specification. Casting a Bird as an AntiTankGrenade would either a) not compile or b) throw a ClassCastException at runtime. This is to enforce some consistency and clarity in code.

On the other hand, In a language such as Perl, conversions are all quite loose. You could do the conversion and have it work without even a cast! Of course, if you've read any Perl written by a true Perl Hacker [tm], you know how obfuscated it can be!


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ClassCastException