• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generics Doubt

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eugenio. Welcome to the Ranch!

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.
 
Ranch Hand
Posts: 32
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Like Matthew said, Generics only exists for the compiler, the JVM doesnt even know what Generics is, all the Generics code are removed after the compilation wich means your methods would be the same
 
Eugenio Anton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. I knew that generics only "work" at compile time and not at runtime. But I never can't imagine that the compiler can anticipate this situation. Thanks a lot.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no its does not compile due to concept of bridge method an after the the erasing both the method looks like void say(List list) so same method in subclass is not allowed error of duplicate method...
 
reply
    Bookmark Topic Watch Topic
  • New Topic