File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics" Watch "generics" New topic


swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 182

This is from KS & BB book..

Please let me know the exact relationship in-between Comparable<DVDInfo> and compareTo(DVDInfo d). How should i read them?
As whenever i try to differ the Comparable's concrete type and compareTo() methods argument, it gives a compile time error.

This is what given in the book: "In line 1 we declare that class DVDInfo implements Comparable in such a way
that DVDInfo objects can be compared to other DVDInfo objects
". which is turning out to be an insufficient information for me..
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446

what you are trying to implement using different types is unknown to us
so please post it here

about line 1
that surely says that, we can compare objects of type DVDInfo

what you have written is correct , DVDInfo objects getting compared means that
we can determine whether two DVDInfo objects are equal or less than or greater than with each other on the basis of its instance variable
for example

compareTo() method is called whenever we sort the array of the DVDInfo objects and that sorting order is determined using the price

hope this is clear to you now

and post your code please

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
swaraj gupta
Ranch Hand

Joined: Oct 22, 2010
Posts: 182

This is what I was trying to i didn't get the point...
case 1:

case 2:
Please let me know the problem with these cases.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

Well, the interface definition is:
So, if you implement Comparable<DVDInfo>, the interface definition tells you that there should be a method compareTo(DVDInfo). Anything else and you'll get a compiler error (unless it's an abstract class) because you simply haven't implemented the interface. As far as the compiler is concerned, it's exactly the same situation as if you'd omitted the compareTo method entirely.
I agree. Here's the link:
subject: generics
jQuery in Action, 3rd edition