File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple casts

 
George Panadis
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been studying for the exam and I started doing some practice tests.
I came across something I haven't seen before (multiple and interface casts) and I am having difficulty finding anything about it online.


Identify options that will compile and run without error.




What are the rules for multiple casts? How do I go about deciphering those?
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3669
9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface cast works exactly the same as a class cast and a multiple cast is not much different from a single one. You should break a multiple into two single casts as shown below:

b = (B)(I) a;

is same as:

I temp = (I) a;
b = (B) temp;

Now, if you know single casts, there should be no issue with the above
 
George Panadis
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay. thank you for the reply.

I wouldn't say im 100% confrotable with object casting but I understand.
Its one of those things that the longer I look at the less sense it makes so I need to get more confortable with them.
 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had asked the same question some 10-12 days ago, you can have a look at answers here:
http://www.coderanch.com/t/593352/java/java/Confused-ClassCastException
http://www.coderanch.com/t/593431/java/java/output-Casting [read Tony Docherty's reply ]

Also look for the reply of Jeff Verdegan on this thread: http://www.coderanch.com/t/566171/java/java/polymorphism-casting-logic#2572095.
He explains the rules of casting in that thread nicely.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic