Author: Nesha Mason

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

  • Calling Generic Methods – Generics

    Calling Generic Methods Consider the following class declaration: Click here to view code image public class ClassDecl {  static <E_1,…, E_k> void genericMethod(P_1 p_1,…, P_m p_m) { … }  // …} Note that in the method declaration above, a type P_i may or may not be from the list of…

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

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

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

  • Functional Interfaces – Functional-Style Programming

    13.1 Functional Interfaces Functional interfaces and lambda expressions together facilitate behavior parameterization (p. 691), a powerful programming paradigm that allows code representing behavior to be passed around as values, and executed when the abstract method of the functional interface is invoked. This approach is scalable, requiring only a lambda expression…

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

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

  • 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 Overloading – Generics

    Implications for Overloading Given the definitions above, we can now state that two methods are overloaded if they have the same name, but their signatures are not override-equivalent. Given the following three generic method declarations in a class: Click here to view code image static <T> void merge(MyStack<T> s1, MyStack<T>…