Home arrow Java arrow Page 4 - Wildcards, Arrays, and Generics in Java

Wildcards, Arrays, and Generics in Java

In this conclusion to a five-part series, you will learn about wildcards, arrays, and more. This article was excerpted from chapter eight of the book Learning Java, third edition, written by Patrick Niemeyer and Jonathan Knudsen (O'Reilly; ISBN: 0596008732). Copyright 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
June 07, 2007
  1. · Wildcards, Arrays, and Generics in Java
  2. · Wildcard Types Versus Generic Methods
  3. · Using Array Types
  4. · Case Study: The Enum Class
  5. · Case Study: The sort() Method

print this article

Wildcards, Arrays, and Generics in Java - Case Study: The Enum Class
(Page 4 of 5 )

If you take a look at the definition of the java.lang.Enum class in Java 5.0, you'll see a rather bizarre-looking generic type declaration:

  Enum< E extends Enum<E> > { ... }

In trying to parse this, you may be hampered by two thoughts, which we'll try to dispel right away. First, upon quick inspection this may appear to be recursive. The type variable E seems to be defined as something that's not yet finished being defined. But it's not really. We often have mathematical equations of the form x = function( x ) and they are not recursive. What they really call out for is a special value of x that satisfies the condition. Next, although it's pretty clear that E is a subtype of some formulation of the generic Enum type, you may jump to the conclusion that E itself must be a generic type. Remember that concrete types can extend generics just as well as generics can.

With these thoughts in mind, let's hunt for some arrangement that satisfies these bounds. Let's focus only on the bound for a moment:

  E extends Enum<E>

E is a subclass of some parameterization of Enum and, in particular, the parameterization of Enum is on the subclass type itself. To say this again, what it does is to require that any invocations of the Enum type are by subclasses of some parameterization of the Enum type. And specifically, the parameterizations of the Enum type supply their own type as the type parameter to their parent, Enum. What kind of class satisfies this condition?

  class Foo extends Enum<Foo> { }

This Foo class does. The declaration of Foo, in fact, reads just as the bound does. Foo is a plain concrete type that extends Enum parameterized by its own type.*

What does this accomplish exactly? The first implication of this arrangement is that Enum can be instantiated only by subclasses of itself. Next, we have the condition that the Enum must be instantiated with the child type as its parameter type. This means that any methods of the parent Enum class that refer to the type variable E will now refer to the child type. This peculiar bound has guaranteed that child types customize their parent with their own type. In fact, this is exactly what the Enum class in Java needs to make enums work. The compareTo() method of a Java enum refers to the type variable and is intended to be applicable only to other instances of the specific child enum type:

  public int compareTo( E e ) { ... }

For example, a Dog enum type should be able to compare only types of Dog and comparing a Dog with a Cat should produce a compile-time error. The bound accomplishes just that by adapting the compareTo() method to the Dog type:

  class Dog extends Enum<Dog> { ... }

Normally, a nonfinal base class, having no way to know what children it may have in the future, could only refer to its own type as a general supertype for all of the children when it wants to work with others of its own kind. Methods of a nongeneric Enum class could only supply methods that work on any Enum. But through the magic of generics, we can effectively change the API of the class based on how it is invoked with parameters. In this case, we arranged that all subclasses must supply themselves as the parameter for the base class, tailoring its methods to themselves and pushing the base type down a generation.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials