public class GenericDemo<E extends GenericDemo> {
E innerE;
public E doStuff(E e, GenericDemo<E> e2) {
//insert code here
}
public E getE() { return innerE; }
}
The options are:
1. return e
2. return e2.getE()
3. return e2
4. return e.getE()
Option (1) and (2) compile fine. Option (3) and (4) give compile error.
Precisely, for
e2
says, incompatible types. Found: GenericDemo<E>. Required: E.
My doubts is that, for e2, isn't E that is required, part of GenericDemo<E> which is e2's type ?For
return e.getE()
says, incompatible types. Found: GenericDemo. Required: E. Here I'm first confused as to how the return type is determined for a method invocation. getE() according to method signature returns E. But invoking it through e.getE() returns GenericDemo?? How is that happening? And even variable e is of the type E? So where is GenericDemo coming from when compiler says found: GenericDemo?And how is option(2) e2.getE() returning E that is actually compiled fine?
I hope I'm able to convey my doubts correctly.