public class Base { <T> List<? extends Number> f1() {return null;} List<? extends Number> f2() {return null;} <T extends Number> List<T> f3() {return null; } } class Derived extends Base { List<String> f1() {return null;}
Why does the definition of the overriding methods f1 () and f3 () in the Derived class not give a compilation error, how is the definition of the redefining methods f2 () in the return type of the Derived class (which gives a compilation error) incompatible with Base.f2 () ")?
The character override rule in JLS allows you to abandon the override method (in the Derived class), and the overridden method (in the base class) is common.
An invalid override rule allows the return type in a subclass of List<String> instead of List<T> in the base class.
But I canโt explain the difference in behavior below, and I donโt understand why f1 () and f3 () override the definitions in the Derived class compile successfully (on Eclipse, SE8), ignoring the restrictions imposed by the limited type parameter for f3 () and the limited wildcard for f1 () !
PS My assumption is that in f1 () and f3 () in the derived compiler it processes both methods, since it returns only a raw list, the compiler first erases (currently only in Derived !?), and then compares these erasable methods in Derived with unerased (so far) in the database. Now the uncontrolled redefinition rule is in order (and there is no need to check the borders - it is simply impossible), the compiler decides that this correct redefinition and compilation goes further ... and somewhere at the end of the compilation generics in Base.f1 () and Base.f3 () is also deleted :)))
This SO answer also adds ideas to this topic.
source share