How do I use paramertized generic types in an inner class?

Will M picture Will M · Jan 6, 2010 · Viewed 21.9k times · Source

I am trying to implement an inner class that has a generic parameterized type.

Here is a short version of my code:

public class AVLTree<T extends Comparable<? super T>> implements Iterable<T> {
 ...

 private class BinaryNode<T extends Comparable<? super T>> {
  ...
 }

 private class TreePreOrderIterator<E extends Comparable<? super E>> implements Iterator<E> {
  ...
 }

}

It does not work. Eclipse/Java is giving me a warning that the type T parameter on the inner class is 'hiding' the super class's parameter. Any thoughts on how I can fix this?

EDIT: I added the other inner class I'm having problems with: TreePreOrderIterator. The generic type T will be the same for AVLTree, BinaryNode, and TreePreOrderIterator. The inner classes need to access fields in AVLTree.

EDIT2: Also, the Iterator accesses a BinaryNode<T>, which is a conflict.

(Note: This is part of bigger project I'm doing for a class. If any other information is needed, please ask.)

Answer

ILMTitan picture ILMTitan · Jan 6, 2010

If you want T in BinaryNode to be the same type as the enclosing T associated with AVLTree, remove the declaration of T in BinaryNode.

If you want the T in BinaryNode to be different than the enclosing T associated with AVLTree, but you want to be able to access properties of the parent AVLTree, rename T to something else.

If you don't need to access properties of the enclosing AVLTree, make BinaryNode static.