Chapter 13

Generic Class Types

WHAT YOU WILL LEARN IN THIS CHAPTER:

  • What a generic type is
  • How you define a generic type
  • How you specify type parameters for a generic type
  • What parameter type bounds are and how you use them
  • What wildcard type specifications are and how you use them
  • How you define bounds for a wildcard
  • How you define and use parameterized methods

Generic class types are not a separate capability from the class and interface types that you have seen in earlier chapters. The facility for defining generic class and interface types is an extension of the ordinary definition of classes and interfaces that you are familiar with that enables you to define families of classes and interfaces.

WHAT ARE GENERIC TYPES?

A generic type, which is also referred to as a parameterized type, is a class or interface type definition that has one or more type parameters. You define an actual class or interface type from a generic type by supplying a type argument for each of the type parameters that the generic type has. It’ll be easier to understand what this means with a concrete example of where and how you could apply the concept.

I’m sure you recall the LinkedList class that you first saw in Chapter 6 and used in an example in Chapter 12. You used the LinkedList class to encapsulate a linked list of Point objects, but the idea of a linked list applies to any set of objects that you want to organize in this way. A linked list is just one example of classes that define objects ...

Get Ivor Horton's Beginning Java®, Java 7 Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.