Saturday, August 22, 2009

Upcasting vs. downcasting

I always seem to forget which is which so I guess I’ll write it down here once and for all.


Casting from a Circle or Rectangle to a Shape is upcasting, casting up in the hierarchy.

Casting from a Shape object to a Circle object is downcasting, casting down in the hierarchy. If you find yourself downcasting, it may be an indication of a code smell, think twice about it!

No comments:

Post a Comment