Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

return type  RSS feed

 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the return type of a method is list...cant we return an arraylist? after all an arraylist is a list?
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes offcourse!!
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if the return type is List we cant assign it to a arraylist....i got it now
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah right, even you cannot return something like
return new List()
because list is an interface you have to return one of its implemented class
Set/ArrayList/LinkedList and so on...
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you see javadocs, ArrayList implements the interface List.

So basically we can say something like





Perfectly legal. Any interface subtype can be returned.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!