This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Try to understando generic methods  RSS feed

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody get to interpret this following code: -> public static <T extends Comparable<? super T>> void sort(List<T> list) ?


André AS
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method accepts a generic list of which the type T must implement (the generic type definition doesn't know implement only extend) Comparable. But the type of Comparable must be T or a super class of T.

Example:
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

André Asantos wrote: Does anybody get to interpret this following code: ->


This says, the sort method accept list of Ts where T must implement comparable interface. Later part actually defined the type of Comparable interface. It says, the comparable interface should be of type of it's base class of current class.

Please see the code below for better understanding.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!