Thursday, 20 January 2011

Java Object Casting

Object casting rules compile time checks:
  • When both OldType and NewType are classes, one class must be a subclass of the other.
  • When both OldType and NewType are arrays, both arrays must contain reference types (not primitives) and it must be legal to cast an element of OldType to an element of NewType
  • You can always cast between an interface and a non-final object.
Object casting checks at runtime:
  • If NewType is a class, the class of the expression being converted must be NewType or must inherit from NewType.
  • If NewType is an interface, the class of the expression being converted must implement NewType.

No comments: