Troubleshooting "The type parameter T is hiding the type T" warning

58,590

Solution 1

Do you somewhere have a class or interface named T, or are you using T as a concrete type name somewhere instead of as a type parameter (which means you might have forgotten somewhere else, for example in an enclosing class, to specify that T is a type parameter)? I can reproduce your problem with this:

class T {  // A concrete type T
}

interface B<T> {  // warning: The type parameter T is hiding the type T
}

interface A<T> extends B<T> {  // warning: The type parameter T is hiding the type T
    T getObject();
}

If I remove class T, it disappears.

Solution 2

Just to expand last answer (I know question is old and closed), I'll provide a Simplest case where you can understand the error:

Imagine an interface with a generic method such as:

<T> Output<T> doSomething(Input<T> input);

If you try to override it as:

@Override
public <Object> Output<Object> soSomething(Input<Object> input){
    /*..*/
}

The compiler warns you:

The type parameter Object is hiding the type Object

What it means is that "Object" is a generic label here, it is not java.lang.Object. If you changed Object type to V or whatever arbitrary letter, that would not happen. But you are using and appelative which collides with an actual defined class, so compiler warns you that this Object of you is hiding what otherwise would be understood as the common Object class.

Solution 3

Even though this question is already answered , since the subject line is about ("The type parameter T is hiding the type T") , just want to add that this warning does not always mean that there is a class or actual type "T" declared somewhere in the classpath (or inner class) as all the answers/comments suggest.

Consider the following:

public class Cache<K extends Comparable<K>,V> {

  private final  ConcurrentHashMap<K,V> _concurrentMap =  new ConcurrentHashMap<K,V>();

  public <K,V> void add(K key ,V  value){ // Compiler warning - The type parameter V is hiding type V (as also for K)

    _concurrentMap.put(key, value); //Compiler error - K and V passed to add are now being considered different
                                    //from whats declared for _concurrentMap variable
  }

  public V get(K key){
    return _concurrentMap.get(key);
  }

  public int size(){
    return _concurrentMap.size();
  }
}

Because K ,V is declared at class level , the method add also "inherits" it. So you end up getting the same warning.

Below removes the warning and compiler error of course

public <E,F> void add(K key ,V  value){ 
    _concurrentMap.put(key, value); 
}

And my original method is this. (Don't require additional generic types for my method)

public void add(K key ,V  value){ 
    _concurrentMap.put(key, value); 
}

Solution 4

I try:

public interface B<T> {
    T getObject();

    public  interface A<T> extends B<T>{
        T getObject();
    }
}

on eclipse Indigo Service Release 2 and there is no warning or error

Share:
58,590
nikdeapen
Author by

nikdeapen

Updated on July 09, 2022

Comments

  • nikdeapen
    nikdeapen almost 2 years

    I am getting a warning in eclipse (the most recent version) for the following code.

    public interface A<T> extends B<T> {
         public T getObject();
    }
    

    The warning appears at 'T' in 'A' and reads: "The type parameter T is hiding the type T".

    The weird part is that the following code generates no errors or warnings.

    public interface A extends B<T> {
         public T getObject();
    }
    

    But now I can't extend A while telling it what type T is.

    I am completely confused. Anyone know why this is happening?

  • nikdeapen
    nikdeapen about 12 years
    That was it, thank you! It turns out I accidentally imported a class with the same name as the type parameter. I took out the import and it worked fine.
  • mgaert
    mgaert over 11 years
    This also happens apparently when an inner generic class re-uses the same Type parameter as the enclosing (also generic) class.
  • Mr Lister
    Mr Lister over 8 years
    Since the order in which answers are displayed is not fixed, "last answer" does not really pinpoint which answer you meant.
  • Whimusical
    Whimusical over 8 years
    Plaiska's one I meant
  • Venkata Raju
    Venkata Raju over 4 years
    I think Eclipse error message would have been more clear/useful if it says The type parameter Object is hiding the type java.lang.Object