Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please explain Generics compilation error

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written the following code:


I am getting compilation error:Bound mismatch: The generic method sort(List<T> ) of type Collections is not applicable for the arguments (ArrayList<Dvd> ) . The inferred type Dvd is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> at Generics1.main.

can anyone explain what Comparable<? super T> means
Thanks
Raj
[ May 29, 2007: Message edited by: raj malhotra ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your DVD class must implement Comparable interface so that sort() method could
sort the object of DVD whose references are staying in the ArrayList.



With Regards,
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

can anyone explain what Comparable<? super T> means


In simple wordings it does mean that it is not must that class T implements
the Comparable interface. If super class of that implements the Comparable
it will also do.

In your case you experiment with what I say:

1- First try by making your Dvd class implement Comparable interface.
2- After that you make a new class and extend the Dvd class from that.
Parent class of Dvd class should implement the Comparable this time so
that you could test the meaning of Comparable<? super T>



Regards,
cmbhatt
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following on from what Chandra wrote: class Dvd can implement interface Comparable<Dvd> simply by having a suitable int compareTo(Dvd dvd) method defined.
[ May 29, 2007: Message edited by: Barry Gaunt ]
 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it.Any class ,which implements Comparable interface or whose super class implements it,can be used to sort using Collections.sort(T o)
Thanks Chandra & Barry
[ May 29, 2007: Message edited by: raj malhotra ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic