Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.)

like image 658
Will M Avatar asked Jan 05 '10 23:01

Will M


People also ask

Can Java inner class be generic?

If I have one class (Class1) which takes the type E and also has an internal class (Class2) which I also want to take type E which should be the same as the E of Class1 in all cases. Class2 is a private internal class, so it will only ever be used by instances of Class1; which means it will never ever have any other E.

What can a generic class be parameterized for?

Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.

How do you declare a generic type in a class?

A Generic Version of the Box Class To update the Box class to use generics, you create a generic type declaration by changing the code "public class Box" to "public class Box<T>". This introduces the type variable, T, that can be used anywhere inside the class.


3 Answers

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.

like image 161
ILMTitan Avatar answered Oct 24 '22 03:10

ILMTitan


Just rename T to something else so it wouldn't conflict with the outer class T.

The type parameter names are visible in all the class scope, including the inner classes. So inner classes or instance methods should declare yet another type parameter whose name conflict with the class type parameter. However, static nested classes and static methods don't have that problem.

like image 22
notnoop Avatar answered Oct 24 '22 03:10

notnoop


Just remove generic type declaration from the BinaryNode class. Since the class is declared private, it can only exist within AVLTree, and therefor you can just use the T declared for AVLTree anywhere inside BinaryNode.

like image 37
Vort3x Avatar answered Oct 24 '22 03:10

Vort3x