12.5 Inheritance and Invariance with Generics
Inheritance and substitution are commonplace concepts for Java developers, so their extension to generics isn’t surprising. The toString() method, for example, is called naturally on all objects, and the call is dynamically bound. Similarly, String.toString(Object o) lets you pass any object, and the static method calls the object method toString() .
12.5.1 Arrays Are Covariant
Let’s take the hierarchy of the wrapper classes as an example. Of course, Object is at the top. The numeric wrapper classes all extend the abstract Number class. Under these wrapper classes are, for example, Integer, Double , and the other numeric wrappers. No headaches should arise in the following example code:
Number ...
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