It is about generics methods in java with two generics types (one for return type and another for a formal parameter) and how to implement it. I guess i am missing something in the picture to get it work.
The matter is this...
This is working :
public enum Getter
{
BillItemsSize {
@Override
public Integer get (Object entity) {
DesiredClass ref = (DesiredClass ) entity; // "Old time" cast
// do things...
}
};
public abstract <T,K> T get (K entity);
}
This is not working :
public enum Getter
{
BillItemsSize {
@Override
public Integer get (DesiredClass entity) { // no cast at all
// do things
}
};
public abstract <T,K> T get (K entity);
}
The java compiler yells me this :
<anonymous datasource.db.Getter$1> is not abstract and does not override abstract method <T,K>get(K) in Getter
Well, that's the situation. Thanks in advance to all!. Hope it helps others in the future!.
P.D: It's not a problem of enums types.. it happens across classes hierarchies. So don't bother blaming the enums, i tried this and doesn't work.
public abstract class SuperClass
{
public abstract <T,E> T pepe (E e);
}
public class SubClass extends SuperClass
{
@Override
public Integer pepe(DesiredClass e) // fails...
{
return null;
}
}
UPDATED:
For Generics parameters
We can make a general rule statement that for "generics parameter" (those whose types is generic) the type implicitly taken in the method signature is equals to upper limits of that generic, that can be Object, if nothing is specified, or a more specific subclass if upper bounds are used (in example T extends String).
For Generics returns types
There is no problem overwriting a generic method with a specific return type, as long the return type is a subtype of the overwritten return type. And what is the return type at first place? Well, it happens to be Object. By default the compiler assumes (in the method signature) the generic type as an Object type.
So we have to thing that the trick is to know that any method that have a generic return type are actually having an Object return type. Then if in any subclasses the method is overwritten and we alter he return type stating that return another type, there will be no problem. Because, besides that method will return an object of another class, that object returned will inevitable be subclass of the Object class and there will be no problem overwrite a method with a different return type that the original as long it be a subtype of the original. The technique called covariant return type allow us to do such thing.
public abstract class SuperClass
{
public abstract <T> T operation ();
}
public class SubClass extends SuperClass
{
@Override
public Chair operation()
{
//bla bla
}
}
meanwhile in another part of the code...
void main ()
{
SubClass sb = new SubClass();
Chair chair = sb.operation ();
// the chair type can be easely replaced by super type (like Object)
Object object = sb.operation();
}
Thanks to all the folks who help clear this out!.
<T,K> get (K)has no implementation whenK === Objectin the second case. - John Dvorak