CS102 · Generics · Annotated SlidesGeneric Classes and Methods
Original
Overview

Generic Classes and Methods

Why generics (one method or class for many types, checked at compile time), generic methods such as printArray and maximum, bounded type parameters (T extends Comparable<T>), and generic classes like Sample<T> and LinkedList<E>.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 18 slides
  1. 1 Slide 1
  2. 2 What are generics methods and generic
  3. 3 Generic methods and generic classes (and interfaces)
  4. 4 Overloaded methods are often used to perform similar
  5. 5 If the operations performed by several overloaded
  6. 6 See code of print array using generics
  7. 7 Generic method maximum determines and return the
  8. 8 Comparable<T> objects have a method.
  9. 9 See code of generic method maximum
  10. 10 The type-parameter section specifies that T extends
  11. 11 interface
  12. 12 Type-parameter declarations that bound the parameter
  13. 13 Class definitions may include parameters for
  14. 14 Legal to use parameter T almost anywhere
  15. 15 Slide 15
  16. 16 Slide 16
  17. 17 LinkedList is an example of a generic class
  18. 18 Check code
Slide 1 / 18

Slide 1

Slide 1: Slide 1
Slide 2 / 18

What are generics methods and generic

Slide 2: What are generics methods and generic
Slide 3 / 18

Generic methods and generic classes (and interfaces)

Slide 3: Generic methods and generic classes (and interfaces)
Slide 4 / 18

Overloaded methods are often used to perform similar

Slide 4: Overloaded methods are often used to perform similar
Slide 5 / 18

If the operations performed by several overloaded

Slide 5: If the operations performed by several overloaded

Notes

  1. A generic method declares its type parameter before the return type: public static <T> void printArray(T[] a).

Exam angle

PredictionQ1

Which header declares a generic method?

Slide 6 / 18

See code of print array using generics

Slide 6: See code of print array using generics
Slide 7 / 18

Generic method maximum determines and return the

Slide 7: Generic method maximum determines and return the
Slide 8 / 18

Comparable<T> objects have a method.

Slide 8: Comparable<T> objects have a method.
Slide 9 / 18

See code of generic method maximum

Slide 9: See code of generic method maximum
Slide 10 / 18

The type-parameter section specifies that T extends

Slide 10: The type-parameter section specifies that T extends

Exam angle

PredictionQ1

In a bounded type parameter you write implements for interfaces, e.g. <T implements Comparable<T>>.

Slide 11 / 18

interface

Slide 11: interface
Slide 12 / 18

Type-parameter declarations that bound the parameter

Slide 12: Type-parameter declarations that bound the parameter
Slide 13 / 18

Class definitions may include parameters for

Slide 13: Class definitions may include parameters for
Slide 14 / 18

Legal to use parameter T almost anywhere

Slide 14: Legal to use parameter T almost anywhere

Notes

  1. Sample<String> s = new Sample<String>(); — the type argument can’t be primitive (use Integer, not int).

Exam angle

PredictionQ1

Which is NOT allowed as a type argument?

Slide 15 / 18

Slide 15

Slide 15: Slide 15
Slide 16 / 18

Slide 16

Slide 16: Slide 16
Slide 17 / 18

LinkedList is an example of a generic class

Slide 17: LinkedList is an example of a generic class
Slide 18 / 18

Check code

Slide 18: Check code