File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic classes and Functions Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • 0
  • 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 ?

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

 
Ankit Garg
Sheriff
Posts: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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...
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • 0
  • 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: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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
  • 0
  • 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
Chrome Eclipse IDE Java
  • 0
  • 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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic