Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Typecaste confusion  RSS feed

 
Saurabh Pillai
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I can call first method like save(myCustomObject); but when I call saveAll(myCustomObjects), (myCustomObjects is List<MyCustomObject>) it gives me compilation error.
.
Why?
 
Stephan van Hulst
Saloon Keeper
Posts: 7172
117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because List<SubType> is not a subtype of List<Type>.

Instead, your method declaration should probably look like:
Read more about it in Oracle's Java Tutorials, the Generics trail.
 
Saurabh Pillai
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!