Troubleshooting “The type parameter T is hiding th

2019-01-08 10:10发布

问题:

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? Thank You.

回答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.



回答2:

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



回答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); 
}


回答4:

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.