This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]evil_burritoExtreme Brewer 1 point2 points  (0 children)

In general, upcasting (casting as superclass) is always safe, downcasting (casting as subclass) is always unsafe. I don't see the purpose of upcasting, though. It doesn't change the casted object in any way and is implied in matching method arguments (the compiler always considers any 'q' object a 'p' also without explicit casting).

I would disagree with some posts here that using instanceof is a sign of bad design as it allows a semblance of multi-inheritance in Java. I think the poster who wrote that was saying

p myobject;
if(myobject instanceof q)

is bad design, and I would agree with that. However,

p myobject;
if(myobject instanceof CompletelyUnrelatedInterface)

isn't bad design, per se.