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

JavaGenericsWarnings

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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 - Java

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 - Java

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 - Java

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

Solution 5 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnikdeapenView Question on Stackoverflow
Solution 1 - JavaJesperView Answer on Stackoverflow
Solution 2 - JavaWhimusicalView Answer on Stackoverflow
Solution 3 - JavaPlaiskaView Answer on Stackoverflow
Solution 4 - JavaOscar CastiblancoView Answer on Stackoverflow
Solution 5 - JavaddcView Answer on Stackoverflow