A Few Notes on the Parameterized Classes
- Often, code is replicated several times where the only difference is the
type of one (or more) of the variables involved.
- Parameterized types allows one to abstract an object class over one or
more of it associations or types.
- In the example, Foo is abstracted over T and class F
instantiates Foo with T at class Window (indicated by the
dotted line). Note that the signatures of the operations are allowed to refer
to T:
- A standard example is list, which might be implemented over integers as shown:
- One might want list over several types (i.e. list of ints, lists of
booleans, lists of lists of ints, ...).
- To accomplish this, we can abstract the list class over type T and then
instantiate it at whichever types we like:
References
- UML Distilled - Fowler & Scott.
Addison-Wesley 1998 (ISBN 0-201-32563-2). Short introduction to UML with
some nice examples.
- GJ: Extending the Java Programming Language with type parameters -
Bracha, Odersky, Stoutamire, Wadler. Manuscript, August 1998. Available
via the GJ homepage
here. Tutorial on Generic Java that nicely illustrates the concepts of
parameterized types.