7.3 Types in Hierarchies
Inheritance involves some new aspects in terms of the compatibility of types. This section deals with the question of which types are compatible and how a type can be tested at runtime.
7.3.1 Automatic and Explicit Typecasting
Let’s recap which types we’ve created so far: We modeled the Nap and Workout classes as subclasses of Event. The custom superclass Event itself doesn’t extend an explicit superclass, so implicitly java.lang.Object is the superclass. Event has two object variables ( about and duration), which are inherited by Nap and Workout, and Workout has the additional variable caloriesBurned for the “burned” calories.
Is-a-Type-of Relationships and Automatic Typecasting
An interesting property associated ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access