Interface ServiceRequest is implemented by concrete class OrderRequest.
When I cast a ServiceRequest object to an OrderRequest.....I get no exceptions.
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
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.
[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
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.