This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Generic classes and Functions Problem

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9674
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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


 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic