Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics Question  RSS feed

 
Krishna doddi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public <T> List<T> meth(List<?> type)
{
System.out.println(type); // 1
return new ArrayList<String>(); // 2
}


Line 1 compiles fine.
Line 2 Fails. Reason : List Required

Cant we return a ArrayList where List is required???
I think we can return the subclass right.

Thanks
Siddharth
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually the error message from the compiler is



It's not saying that you can't return an ArrayList.

It's saying it expects you to return a List whose elements are the type parameter T.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!