• 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

Generic classes and Functions Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Line 1 compiles fines individually but gives error when line 2,3 are also used

Why?

-----------------------------------------------



can someone tell me why doesn't all the function
declaration in the above class fails to compile ?

---------------------------------------

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Line 1 compiles fines individually but gives error when line 2,3 are also used. Why?


You cannot overload methods based on different generic types. Like I cannot have these methods in the same class
This limitation is due to type-erasure as at runtime these two methods would be completely same...
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:
This limitation is due to type-erasure as at runtime these two methods would be completely same


ok,at runtime typing information doesn't exist, but at compile time,compiler can able to identify the different types(type parameter),then why it doesn't compile?
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:At compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??


hmm..ok,got the concept,JVM(instance of JRE) which doesn't allow program to compile here in this case.
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit wrote:
at compile time the compiler can identify which method will be called but how will the JVM identify which method to call at runtime?? It will have two methods with the exact same signature, which method would it call??




I didn't get it.
can anyone explain


 
It's just a flesh wound! Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic