Category: Calling Generic Methods

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

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

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

  • Implications for the instanceof operator – Generics

    Implications for the instanceof operator Although the discussion here is about the instanceof type comparison operator, it applies equally to the instanceof pattern match operator. At (1) below, we want to determine whether the object referenced by obj is an instance of the concrete parameterized type MyStack<Integer>—that is, whether it…

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

  • Other Implications – Generics

    Other Implications Enum Types Because of the way enum types are implemented using the java.lang.Enum class, we cannot declare a generic enum type: Click here to view code image enum CoinToss<C> { HEAD, TAIL; }                  // Compile-time error! An enum type can implement a parameterized interface, just like a non-generic…