Shengjie
Shengjie

Reputation: 12796

Java generics - The type parameter String is hiding the type String

In my interface:

public <T> Result query(T query)

In my 1st subclass:

public <HashMap> Result query(HashMap queryMap)

In my 2nd subclass:

public <String> Result query(String queryStr)

1st subclass has no compilation warning at all while 2nd subclass has: The type parameter String is hiding the type String? I understand my parameter is hidden by the generics type. But I want to understand underneath what exactly happened?

Upvotes: 9

Views: 13927

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198211

It thinks you're trying to create a type parameter -- a variable -- whose name is String. I suspect your first subclass simply doesn't import java.util.HashMap.

In any event, if T is a type parameter of your interface -- which it probably should be -- then you shouldn't be including the <String> in the subclasses at all. It should just be

public interface Interface<T> {
  public Result query(T query);
}

public class Subclass implements Interface<String> {
  ...
  public Result query(String queryStr) { 
    ...
  }
}

Upvotes: 17

Related Questions