MsA
MsA

Reputation: 2979

Bound mismatch error while dealing with generics and inheritance

I have following classes and interfaces:

public class Entity {}
public interface EntitySet<T extends Entity> {}

Now I want to write class implementing EntitySet interface and reusing type T. I tried following:

public class Method1EntitySet<T> implements EntitySet<T extends Entity>{}

This gives me error:

Syntax error on token "extends", , expected

So I tried:

public class Method1EntitySet<T> implements EntitySet<T>{}

This gives me error:

Bound mismatch: The type T is not a valid substitute for the bounded parameter of the type EntitySet

This works:

public class Method1EntitySet<T> implements EntitySet{} 

but it gives me warning:

EntitySet is a raw type. References to generic type EntitySet should be parameterized.

Also I guess, above T is not forced to extend Entity.

How should I get this done?

Upvotes: 1

Views: 79

Answers (1)

Eran
Eran

Reputation: 393811

The type bound should be where the generic type parameter T is declared:

public class Method1EntitySet<T extends Entity> implements EntitySet<T>{}

Upvotes: 4

Related Questions