This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I don't understand the answer to this question. The question is if the code below compiles successfully?
My first answer was "yes, of course it compiles, but I don't know if it is overriding or overloading".
But when I qualify my exam I saw that my answer was wrong and the code doesn't compile. I proved it on Eclipse and the compiler said "Name clash: The method say(List<Integer>) of type Child has the same erasure as say(List<String>) of type Parent but does not override it".
I don't know what it means, someone can told me please? Thanks.
When generics were introduced into Java (version 1.5) they decided to do it in a way that would make it backwards compatible. This was done using type erasure, which means that the generic information is only used an compile time, not an run time.
So in this particular example, at run time these methods both have a signature void say(List list) (which is what it means to say they have "the same erasure"). If it was allowed, it would have to involve overriding. But then you lose type safety.
The compiler has access to the generic types. So it can tell that overriding shouldn't be allowed here. But it's not able to treat this an an overloaded method because it will compile down to the same signature. The only safe approach is to not allow this at all, and raise a compiler error.