Object Type Casting

  • Similarity with primitive conversion

    Converting from one type to another

  • Difference

    Primitive type variables store values. So when we convert from a larger to type to smaller type, we might end up loosing information.

    Reference variables on the other hand do not contain the object itself, but its reference. So when we convert types of objects, we're not changing the object, but we're just changing the label on the object, expanding or narrowing the opportunities to work with.

    Upcasting narrows the list of methods and properties available to this object and down casting can extend it.


Children
  1. Class Cast Exception
  2. Downcasting
  3. Upcasting