53

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?

İsmail Y.
  • 3,579
  • 5
  • 21
  • 29
nikdeapen
  • 1,581
  • 3
  • 15
  • 27
  • 9
    In my case Eclipse had automatically imported `org.apache.poi.ss.formula.functions.T`, which caused the warning. – Thomas Ahle Jul 07 '14 at 11:57

5 Answers5

64

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.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • 2
    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. – nikdeapen Apr 04 '12 at 13:40
  • 22
    This also happens apparently when an inner generic class re-uses the same Type parameter as the enclosing (also generic) class. – mgaert Jan 16 '13 at 14:19
7

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.

Whimusical
  • 6,401
  • 11
  • 62
  • 105
6

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); 
}
Plaiska
  • 166
  • 2
  • 11
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

Oscar Castiblanco
  • 1,626
  • 14
  • 31
0

Just Removing the generic <T> in front of return type removed the warning.

This is the earlier code :

private <T> LocalDate convertToDate(Root<T> root, String name, String value) {
    ..........
    ..........
    return LocalDate.parse(value, dtf);
}

Warning disappeared with below

private LocalDate convertToDate(Root<T> root, String name, String value) {
    ..........
    ..........
    return LocalDate.parse(value, dtf);
}
ddc
  • 885
  • 6
  • 12