• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic - Type Erasure removes information (at compile time) required at runtime. How does it work?

 
Kamal Tripathi
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the follow code for Generic. Because of erasure property I defined the methods differently.



However since the compiler erases the generics these classes will not carry this information forward so does this mean if packaged in a jar I will not be able to call these methods. I know that in real world scenario I am still able to call and get good results but how is "type erasure" effective if this information is deleted during compilation yet when it is required at runtime.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kamal Tripathi wrote:how is "type erasure" effective...

Type erasure is effective at allowing us to mix legacy code with generics code, however, due to the design of your code, mixing legacy code with generics code will lead to an 'ambiguity' compiler error.

Kamal Tripathi wrote:...when it is required at runtime

It is not required at runtime. At compile time, the compiler removes all information related to type parameters and type arguments. It is able to do this because this information is not required at runtime. At compile time, the compiler decides which overloaded method will be called.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic