Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

generics

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?

like image 891
Shengjie Avatar asked Apr 16 '12 16:04

Shengjie


1 Answers

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) { 
    ...
  }
}
like image 194
Louis Wasserman Avatar answered Oct 16 '22 03:10

Louis Wasserman