Casting and Converting Reference Types

Classes and arrays are known as reference types in Java. These types are always handled with references. Unlike C/C++, you cannot dereference an object to work with it by value instead. When you are using reference types, it is often necessary to cast or convert an object to another type.

One strength of inheritance is that you can treat an instance of a subclass the same as an instance of its parent class. You typically see this when you pass an object to a method that expects a parameter whose type is that of the object's superclass. This might look like a parameter mismatch in your code but the compiler automatically handles the use of a subclass in place of one of its superclasses by converting the reference. ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.