Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic method doubt

 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't the following code compile?


What is wrong with <T extends A> ??
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Look at your method signature above and then look at what you are attempting to pass to the go method:



Notice the type for variable l?
 
Gaurangkumar Khalasi
Ranch Hand
Posts: 187
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try out with
instead of
And try to figure out your problem!!!
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurangkumar Khalasi wrote:Try out with
instead of
And try to figure out your problem!!!


Got it! Thanks gaurang!!
 
Kedar Pethe
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:

Look at your method signature above and then look at what you are attempting to pass to the go method:



Notice the type for variable l?


Understood.. i should have passed the instance variable of anything that extends A, instead!!
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you are passing a List<T> but the method is expecting T.
 
Joe Harry
Ranch Hand
Posts: 10084
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:No, you are passing a List<T> but the method is expecting T.


And that was exactly what the compiler told you. Have a look at the following page for a much detailed explanation on Generics. Worth the read!

http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic