Category: Overriding Methods from Non

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

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

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

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

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

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

  • Modifying an ArrayList – Collections, Part I: ArrayList

    12.3 Modifying an ArrayList<E> The ArrayList<E> class provides methods to append, insert, replace, and remove elements from a list. In addition, it has methods to modify the capacity of a list. Adding Elements The various add methods allow elements to be appended at the end of a list and also…

  • Lists – Collections, Part I: ArrayList

    12.1 Lists Once an array is created, its length cannot be changed. This inflexibility can be a significant drawback when the amount of data to be stored in an array is not known a priori. In Java, the structures known as lists alleviate this shortcoming. Lists are collections that maintain…