I will ask a very short question and hope that someone can help me with the solution. It describes generic methods in Java with two types of generics (one for the return type and the other for the formal parameter) and how to implement it. I guess I am missing something in the picture to make it work.
The fact is that...
This works :
public enum Getter { BillItemsSize { @Override public Integer get (Object entity) { DesiredClass ref = (DesiredClass ) entity;
This does not work :
public enum Getter { BillItemsSize { @Override public Integer get (DesiredClass entity) {
The java compiler yells to me:
<anonymous datasource.db.Getter$1> is not abstract and does not override abstract method <T,K>get(K) in Getter
Good thing the situation is. Thanks in advance to everyone !. Hope this helps others in the future!
PD: This is not an enumeration type problem .. it happens across class hierarchies. So don’t worry blaming emuns ... I have tried this and it doesn’t work either.
public abstract class SuperClass { public abstract <T,E> T pepe (E e); } public class SubClass extends SuperClass { @Override public Integer pepe(DesiredClass e)
UPDATED:
For Generics Options
We can make a rule of the general rule that for a “generics parameter” (those whose types are common), a type implicitly accepted in the method signature is equal to the upper limits of this general, which can be Object, if nothing is specified, or a more specific subclass, if upper bounds are used (in the example T extends String).
For Generics returns types
There is no problem rewriting a generic method with a specific return type, since the return type is a subtype of the rewritable return type. And what is the return type in the first place? Well, it turns out to be an object. By default, the compiler assumes (in the method signature) a generic type as an object type.
So, we have to say that the trick is to know that any method that has a common return type is actually an Object return type. Then, if the method is overwritten in any subclasses, and we change its return type, indicating that we return another type, there will be no problems. Because, besides the fact that this method will return an object of another class, this return object will inevitably be a subclass of the Object class, and the problem will not be overwritten with another type of return type, which the original must be a subtype of the original, A method called covariant return type allows us to do such a thing.
public abstract class SuperClass { public abstract <T> T operation (); } public class SubClass extends SuperClass { @Override public Chair operation() {
meanwhile in another piece of code ...
void main () { SubClass sb = new SubClass(); Chair chair = sb.operation ();
Thanks to everyone who helps to understand this!