Java generics - String type parameter hides String type

In my interface:

public <T> Result query(T query) 

In my first subclass:

 public <HashMap> Result query(HashMap queryMap) 

In my second subclass:

 public <String> Result query(String queryStr) 

The 1st subclass has no compilation warning at all, and the second subclass: Does a String parameter hide the String type? I understand that my parameter is hidden by generics type. But I want to understand what exactly happened?

+7
source share
1 answer

He thinks you're trying to create a type parameter - a variable - whose name is String . I suspect your first subclass just does not import java.util.HashMap .

In any case, if T is a parameter of the type of your interface, which probably should be, then you should not include <String> in subclasses at all. It should be easy

 public interface Interface<T> { public Result query(T query); } public class Subclass implements Interface<String> { ... public Result query(String queryStr) { ... } } 
+15
source

All Articles