6.4. Casting

[7.3] Determine when casting is necessary

Casting is the process of forcefully making a variable behave as a variable of another type. If a class shares an IS-A or inheritance relationship with another class or interface, their variables can be cast to each other’s type.

In section 6.3, you learned that you can’t access all the members of the class HRExecutive (derived class) if you refer to it via a variable of type Interviewer (implemented interface) or Employee (base class). In this section, you’ll learn how to cast a variable of type Interviewer to access variables defined in the class HRExecutive and why you’d want to.

6.4.1. How to cast a variable to another type

We’ll start with the definitions of the interface ...

Get OCA Java SE 8 Programmer I Certification Guide 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.