1

I have an interface:

public interface Handler {
public <T> boolean shouldProcess(final T obj);

and implementation:

public class SampleHandler implements Handler {
@Override
public <Long> boolean shouldProcess(final Long date) {
 return <some comparison>;
}

I am getting "The type parameter Long is hiding the type Long". What is the reason I am getting this?

rgettman
  • 176,041
  • 30
  • 275
  • 357
Tejas Joshi
  • 197
  • 3
  • 12

3 Answers3

6

Because you're not instantiating T with the concrete type Long; you're actually declaring a new type variable called Long which has the same name as regular Longs and thus shadows them. I suspect you meant:

public interface Handler<T> {
  public boolean shouldProcess(final T obj);
}

public class SimpleHandler implements Handler<Long> {
  @Override
  public boolean shouldProcess(final Long date) {
    ...
  }
}
jacobm
  • 13,790
  • 1
  • 25
  • 27
2

<Long> is a generic type parameter which happens to have the same name as the java.lang.Long class. Therefore it hides that class, so the (final Long date) argument of your method is of generic type and not of the java.lang.Long type.

If you want SampleHandler to implement Handler using java.lang.Long for the generic type parameter, I suggest you move the type parameter to the interface/class level:

public interface Handler<T> {
    public boolean shouldProcess(final T obj);
}

public class SampleHandler implements Handler<Long> {
    @Override
    public boolean shouldProcess(final Long date) {
        return <some comparison>;
    }
}
Eran
  • 387,369
  • 54
  • 702
  • 768
1

Long or java.lang.Long is a built-in class.

Try not to use generic types, or custom classes, or variables which are the same name as common classes.

More likely you want to have a Handler which knows it's type, otherwise, it's much the same as using Object

public interface Handler<T> {
    public boolean shouldProcess(final T obj);

public class SampleHandler implements Handler<Long> {
    @Override
    public boolean shouldProcess(final Long date) {
        return <some comparison>;
    }
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130