• Post Reply Bookmark Topic Watch Topic
  • New Topic

Typecaste confusion

 
Saurabh Pillai
Ranch Hand
Posts: 527
  • 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
Bartender
Posts: 6646
90
  • 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: 527
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Stephan.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!