• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on generics - from Niko questions

 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When i try to compile the above code, it fails.
say(Set<Double> set) and say(Set<Boolean> set) is not a valid overload??
can any one explain why?

also,

can anyone explain, why the instanceof examples below give compiler error???

a)

b)
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you might know, generics is only for compile time type safety. The JVM doesn't know about generics. So if you create a List<String>, to the JVM its the same as List without any types. That's why when you write two methods like
Here after type erasure both the methods will have the same signature, so there is no overloading and thus you get an error. You can find more details about overloading here and about instanceof here...
 
Anuradha Prasanna
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ankit, i will look into it..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic