Which header declares a generic method?
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 Slide 1
- 2 What are generics methods and generic
- 3 Generic methods and generic classes (and interfaces)
- 4 Overloaded methods are often used to perform similar
- 5 If the operations performed by several overloaded
- 6 See code of print array using generics
- 7 Generic method maximum determines and return the
- 8 Comparable<T> objects have a method.
- 9 See code of generic method maximum
- 10 The type-parameter section specifies that T extends
- 11 interface
- 12 Type-parameter declarations that bound the parameter
- 13 Class definitions may include parameters for
- 14 Legal to use parameter T almost anywhere
- 15 Slide 15
- 16 Slide 16
- 17 LinkedList is an example of a generic class
- 18 Check code
If the operations performed by several overloaded
Notes
A generic method declares its type parameter before the return type: public static <T> void printArray(T[] a).
Exam angle
The type-parameter section specifies that T extends
Exam angle
In a bounded type parameter you write implements for interfaces, e.g. <T implements Comparable<T>>.
- Always extends.
Legal to use parameter T almost anywhere
Notes
Sample<String> s = new Sample<String>(); — the type argument can’t be primitive (use Integer, not int).
Exam angle
Which is NOT allowed as a type argument?