Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes please explain Generics compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "please explain Generics compilation error " Watch "please explain Generics compilation error " New topic
Author

please explain Generics compilation error

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
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

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: please explain Generics compilation error
 
Similar Threads
Urgent need Help Sorting Collections
Generic question
Need Help
Linked List need help
size of list in JSTL