Category: Implications for Nested Classes
-
Implications for Overloading – Generics
•
Implications for Overriding The following conditions (referred to as override criteria) should be satisfied in order for a subtype method to override a supertype method: Here we discuss the implication of method signatures for overriding. The @Override Annotation We can solicit the aid of the compiler to ensure that a…
-
Comparing Unmodifiable Lists and List Views – Collections, Part I: ArrayList
•
Comparing Unmodifiable Lists and List Views There are subtle differences to be aware of between unmodifiable lists and list views. The Arrays.asList() method returns a fixed-size list view that is backed by the array passed as an argument so that any changes made to the array are reflected in the…
-
Overriding Methods from Non-Generic Supertype – Generics
•
Overriding Methods from Non-Generic Supertype In Example 11.13, the signature at (1′) is the same as the signature at (1): set(Integer). The signature at (2′) is the same as the erasure of the signature at (2): set(List). The method at (2′) shows a non-generic subtype method overriding a supertype method…
-
Primitive Values and ArrayLists – Collections, Part I: ArrayList
•
Primitive Values and ArrayLists Since primitive values cannot be stored in an ArrayList<E>, we can use the wrapper classes to box such values first. In the following code, we create a list of Integer in which the int values are autoboxed in Integer objects and then added to the list.…
-
Implementing a Simplified Generic Stack – Generics
•
11.8 Implementing a Simplified Generic Stack The Node<E> class from Example 11.2, p. 568, can be used to implement linked data structures. Example 11.10 is an implementation of a simplified generic stack using the Node<E> class. The emphasis is not on how to develop a full-blown, industrial-strength implementation, but on…
-
Type Erasure – Generics
•
11.11 Type Erasure Understanding translation by type erasure aids in understanding the restrictions and limitations that arise when using generics in Java. Although the compiler generates generic-free bytecode, we can view the process as a source-to-source translation that generates non-generic code from generic code. The translated code has no information…
-
Genericity and Inherited Methods – Generics
•
Genericity and Inherited Methods The subsignature requirement for overriding means that the signature of the subtype method must be the same as that of the supertype method, or it must be the same as the erasure of the signature of the supertype method. Note the implication of the last sentence:…
-
Implications for Casting – Generics
•
Implications for Casting A non-reifiable type can lose important type information during erasure and the cast may not have the desired effect at runtime. A cast to a non-reifiable type is generally flagged as an unchecked cast warning, and the cast is replaced by a cast to its erasure. Again,…
-
Creating Unmodifiable Lists – Collections, Part I: ArrayList
•
Creating Unmodifiable Lists Unmodifiable collections are useful to prevent a collection from accidently being modified, as doing so might cause the program to behave incorrectly. Such collections are also stored efficiently, as no bookkeeping is required to support any further modifications and data in the collection can be packed more…
-
Implications for Exception Handling – Generics
•
Implications for Exception Handling When an exception is thrown in a try block, it is matched against the parameter of each catch block that is associated with the try block. This test is similar to the instance test, requiring reifiable types. The following restrictions apply, and are illustrated in Example…