• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

please explain Generics compilation error

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic