Category: Implications for Overloading

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

  • Creating List Views – Collections, Part I: ArrayList

    12.7 Creating List Views The asList() method in the Arrays class and the toArray() methods in the Collection<E> interface provide the bidirectional bridge between arrays and collections. The asList() method of the Arrays class creates List<E> views of arrays. Click here to view code image @SafeVarargs <E> List<E> asList(E… elements)                    From Arraysclass.…

  • Wildcard Parameterized Types as Formal Parameters – Generics

    Wildcard Parameterized Types as Formal Parameters We now examine the implications of using wildcard parameterized types to declare formal parameters of a method. We want to add a method in the class MyStack<E> (Example 11.10, p. 598) for moving the elements of a source stack to the current stack. Here…

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

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

  • Declaring References and Constructing ArrayLists – Collections, Part I: ArrayList

    12.2 Declaring References and Constructing ArrayLists In the discussion that follows, we assume that any class or interface used from the java.util package has been imported with an appropriate import statement. The code below illustrates how we can create an empty ArrayList of a specific element type, and assign its…

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

  • Iterating Over an ArrayList – Collections, Part I: ArrayList

    12.5 Iterating Over an ArrayList<E> Various methods for iterating over collections are discussed in §15.2, p. 791. Here we look at a very common task of iterating over a list to perform some operation on each element of the list. We can use positional access to iterate over a list…

  • Implications for Arrays – Generics

    Implications for Arrays Array store checks are based on the element type being a reifiable type, in order to ensure that subtype covariance between array types is not violated at runtime. In the code below, the element type of the array is String and the array store check at (1)…

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