Generic Classes
Many developers will view themselves primarily as consumers of generics. However, as you get
more comfortable with generics, you're likely to find yourself introducing your own generic classes
and frameworks. Before you can make that leap, though, you'll need to get comfortable with all the
syntactic mutations that come along with creating your own generic classes. The goal of this chap-
ter, then, is to dig into all of the details associated with building generic classes, explaining how
generics extend the existing rules for defining and consuming classes. Fortunately, as you move
through this chapter, you'll notice that the syntax rules for defining generic classes follow many of
the same patterns you've already grown accustomed to with non-generic types. So, although there
are certainly plenty of new generic concepts you'll need to absorb, you're likely to find it quite easy
to make the transition to writing your own generic types.
Parameterizing Types
In a very general sense, a generic class is really just a class that accepts parameters. As such, a
generic class really ends up representing more of an abstract blueprint for a type that will, ulti-
mately, be used in the construction of one or more specific types at run-time. This is one area
where, I believe, the C++ term templates actually provides developers with a better conceptual
model. This term conjures up a clearer metaphor for how the type parameters of a generic class
serve as placeholders that get replaced by actual data types when a generic class is constructed.
Of course, as you might expect, this same term also brings with it some conceptual inaccuracies
that don't precisely match generics.
The idea of parameterizing your classes shouldn't seem all that foreign. In reality, the mindset
behind parameterizing a class is not all that different than the rationale you would use for parame-
terizing a method in one of your existing classes. The goals in both scenarios are conceptually very
similar. For example, suppose you had the following method in one of your classes that was used
to locate all retired employees that had an age that was greater than or equal to the passed-in
parameter (
minAge
):
07_559885 ch04.qxd 9/8/05 11:01 PM Page 47
Summary :
Generic Classes Many developers will view themselves primarily as consumers of generics. Parameterizing Types In a very general sense, a generic class is really just a class that accepts parameters. This term conjures up a clearer metaphor for how the type parameters of a generic class serve as placeholders that get replaced by actual data types when a generic class is constructed.
Tags :
classes,class,types,all,generics,term,youll,get,own,parameterizing,used,ery,need