.. This file is part of the OpenDSA eTextbook project. See .. http://opendsa.org for more details. .. Copyright (c) 2012-2020 by the OpenDSA Project Contributors, and .. distributed under an MIT open source license. .. avmetadata:: :author: Molly Domino Generics 2 ========== Objectives ---------- Upon completion of this module, students will be able to: * Determine when a generic needs to be bounded * Write and use methods and classes that use bounded generic parameters * Write and use bounded generic methods * Become familiar with syntax for wildcards More on Generics ---------------- Code Examples ~~~~~~~~~~~~~ .. admonition:: Try It Yourself In Eclipse, use the *Project > Download Assignment...* menu command to download the exercise project named "ex10.01-Generics". Use this example to follow along with the following video. Feel free to experiment. Refer to `01.02: Lab: LightBot for Beginners `_ if you need to review the instructions for downloading Eclipse projects. [10:53] Generics 2 ~~~~~~~~~~~~~~~~~~ .. raw:: html
.. raw:: html Video Slides 10.1.2.1-MoreOnGenerics.pdf [17:26] Reflecting on Generics ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. raw:: html
Related Resources ~~~~~~~~~~~~~~~~~ `A tutorial on using wildcards in generics, at https://docs.oracle.com/javase/tutorial/java/generics/wildcards.html `_ Sample Declarations of Generic Methods Explained ------------------------------------------------ **The format for declaring a Generic method is as follows:** .. code-block:: java methodModifiers returnType methodName(methodParameters) Note the use of the generic parameters placed inside the angle brackets. **Example 1** Below is one example of how you may declare a Generic method. .. code-block:: java public static void sort(T[] items, Comparator comp) The T following the static keyword and enclosed within the angle brackets represents the generic parameter for the sort method. The T should also appear in the method parameter list. The second method parameter ``Comparator comp`` is our way of specifying that ``comp`` must be an object that implements the ``Comparator`` interface for type ``T`` or for a superclass of type ``T`` We use this approach to specify restrictions, for example, you can define a class that implements ``Comparator`` and use it to sort an array of Integer objects or an array of Double objects **Example 2** Below is another example Generic method declaration. .. code-block:: java public static > void sort(List list) The use of ``>`` specifies that the generic parameter ``T`` must implement the interface ``Comparable``. The method parameter list (the object being sorted) is of type ``List``. Checkpoint 1 ------------ .. avembed:: Exercises/SWDesignAndDataStructs/Generics2Checkpoint1Summ.html ka :long_name: Checkpoint 1 Bounded Wildcard Examples ------------------------- Code Example ~~~~~~~~~~~~ .. admonition:: Try It Yourself In Eclipse, use the *Project > Download Assignment...* menu command to download the exercise project named "ex10.01-Generics". Use this example to follow along with the following video. Feel free to experiment. Refer to `01.02: Lab: LightBot for Beginners `_ if you need to review the instructions for downloading Eclipse projects. [10:43] Bounded Wildcards Example ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .. raw:: html
Related Resources ~~~~~~~~~~~~~~~~~ * `https://docs.oracle.com/javase/tutorial/java/generics/wildcards.html `_ * `https://docs.oracle.com/javase/tutorial/java/generics/bounded.html `_ Programming Practice: Generics 1 -------------------------------- .. extrtoolembed:: 'Programming Practice: Generics 1' :workout_id: 1919