I'm trying to use generic methods in java for the first time. It isn't working, and it appears to me that Java generics are simply not meant to be used this way - that I will need to implement a common interface rather then using a generic method. Please confirm (or deny) that conclusion.
This is what I'm trying to do:
public <T> void updateItem( T item ) {
String itemKey = item.getKey();
...
This gives me the error 'The method getKey() is undefined for the type T'.
Looking into why this doesn't work I see that type erasure "replaces all type parameters in generic types with their bounds or Object if the type parameters are unbounded".
The only way I can 'bound' my type T is if I create a common subclass or interface for all the types I was planning to use, and if I do that then I can use polymorphism rather then generics.
<T extends SomeClassWithGetKey>