Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics.

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When I am changing the argument of addAnimal() from addAnimal(ArrayList<Animal> animals) to addAnimal(List<Animal> animals)
Why is the code compiling and running with out errors?

I know, in the first case its the actual object and in the second case its a reference type but I cant understand how it's making a difference.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: List is a interface, ArrayList is a class that implements List
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already know that...
How does it make a differnece....
Please be more clear..I am having a hard time figuring this topic...
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are passing a List reference to an ArrayList. It wont work List IS-NOT-A ArrayList the other way is true. Hope the below code helps

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes a lot of sense...!

Thanks alot..
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic