Arrays and the Class Hierarchy
At the end of Chapter 4, we mentioned that arrays have a place in the Java class hierarchy, but we didn’t give you any details. Now that we’ve discussed the object-oriented aspects of Java, we can give you the whole story.
Array classes live in a parallel Java class hierarchy under the
Object
class. If a class is a direct subclass of Object,
then an array class for that base type also exists as a direct
subclass of Object. Arrays of more derived classes
are subclasses of the corresponding array classes. For example,
consider the following class types:
class Animal { ... }
class Bird extends Animal { ... }
class Penguin extends Bird { ... }
Figure 6.8 illustrates the class hierarchy for
arrays of these classes. Arrays of the same dimension are related to
one another in the same manner as their base type classes. In our
example, Bird is a subclass of
Animal, which means that the
Bird[] type is a subtype of
Animal[]. In the same way a
Bird object can be used in place of an
Animal object, a Bird[] array
can be assigned to a variable of type Animal[]:
Animal [][] animals; Bird [][] birds = new Bird [10][10]; birds[0][0] = new Bird( ); // make animals and birds reference the same array object animals = birds; Observe( animals[0][0] ); // processes Bird object

Figure 6-8. Arrays in the Java class hierarchy
Because arrays are part of the class hierarchy, we can ...
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