Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not super in generic method declaration

 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This script is from examlab final:



My question, if I change the method declaration from <T extends E> to <T super E>, it won't compile. Why not?
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the super keyword only accepts the wildcard as the left operand.
Am I right?
 
Henry Wong
author
Marshal
Pie
Posts: 21385
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
zhong chen wrote:
My question, if I change the method declaration from <T extends E> to <T super E>, it won't compile. Why not?


Let's assume, for a moment, that <T super E> is allowed. What would that get you? You code will need to handle any Object type, as Object is a super of E, so how is that different from not using method generics?

Henry
 
zhong chen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank make sense. Thanks to you both!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic