Category: Other Implications

  • 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…

  • Arrays versus ArrayLists – Collections, Part I: ArrayList

    12.8 Arrays versus ArrayLists Table 12.1 summarizes the differences between arrays and ArrayLists. Table 12.1 Summary of Arrays versus ArrayLists   Arrays ArrayList Construct support Built into the language. Provided by the generic class ArrayList<E>. Initial length/size specification Length is specified in the array construction expression directly or indirectly by…

  • Implications for Nested Classes – Generics

    Implications for Nested Classes Nested classes and interfaces can be declared as generic types, as shown in Example 11.19. All nested generic classes, except anonymous classes, can specify formal type parameters in their declaration, as at (2) through (6). Anonymous classes do not have a name, and a class name…

  • Implications for Non-Reifiable Variable Arity Parameter – Generics

    Implications for Non-Reifiable Variable Arity Parameter Because variable arity parameters are treated as arrays, generics have implications for non-reifiable variable arity parameters (T … varargs). Most of the workarounds for arrays are not applicable, as array creation is implicit for variable arity parameters. In a method declaration with a non-reifiable…

  • 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…

  • Capture Conversion – Generics

    Capture Conversion Consider the following non-generic method which does not compile: Click here to view code image static void fillWithFirstV1(List<?> list) {    Object firstElement = list.get(0);       // (1)    for (int i = 1; i < list.size(); i++)      list.set(i, firstElement);             // (2) Compile-time error  } The method should fill any list…

  • Bridge Methods – Generics

    Bridge Methods Bridge methods are inserted in subclasses by the compiler to ensure that overriding of methods works correctly. The canonical example is the implementation of the Comparable interface. The post-erasure code of the class CmpNode<E> from Example 11.8 is shown below. A second compareTo() method has been inserted by…

  • 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:…

  • Replacing Elements – Collections, Part I: ArrayList

    Replacing Elements The following methods replace elements in a list with new elements. Click here to view code image E set(int index, E element)                         From List<E>interface. Replaces the element at the specified index with the specified element. It returns the previous element at the specified index. The method throws an IndexOutOfBoundsException if…

  • Limitations and Restrictions on Generic Types – Generics

    11.13 Limitations and Restrictions on Generic Types In this section we take a look at implications and restrictions on generic types for instance tests, casting, arrays, variable arity parameters, exception handling, nested classes, and enum types. Reifiable Types Concrete parameterized types are used by the compiler and then translated by…