November 2024
Intermediate to advanced
499 pages
12h 13m
English
You often need to implement classes and methods that work with multiple types. For example, an ArrayList<T> stores elements of an arbitrary class T. We say that the ArrayList class is generic, and T is a type parameter. The basic idea is very simple and incredibly useful. The first two sections of this chapter cover the simple part.
In any programming language with generic types, the details get tricky when you restrict or vary type parameters. For example, suppose you want to sort elements. Then you must specify that T provides an ordering. Furthermore, if the type parameter varies, what does that mean for the generic type? For example, what should be the relationship between ArrayList<String> to a method that ...
Read now
Unlock full access