This week's book giveaway is in the Java 9 forum.
We're giving away four copies of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications and have Sander Mak & Paul Bakker on-line!
See this thread for details.
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Typecaste confusion  RSS feed

 
Saurabh Pillai
Ranch Hand
Posts: 541
  • 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: 7927
143
  • 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: 541
  • 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!