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

Comparable interface

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69

I don't quite understand the codes above. I mean I know what it means, but I don't know details. So comparable is an interface here. I don't really know what interface does? Just provides more methods for the class? My teacher said if interface is comparable, there must be a compareTo method --public int compareTo(Object obj) Object is a super class that contains many objects, but why we need that here? (Object obj)?
Line 23, What does this statement mean? (Lecture) is casting? what is 'other'?
Line 24, Is the 'other' here same as the above? But how does the program know which end time is other, which end time is this??
For this program, user gives input of start time and end time. For example, 9.10(means 9.10 am)-10.23(means 10:23 am).
Line 15-19, I know what toString method does, but why we need it here? Why we need to convert double Starttime and end time to string? And plus, I didn't use variable description in later classes. Thanks for helping out.
Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Cheryl Scodario wrote:. So comparable is an interface here. I don't really know what interface does? Just provides more methods for the class?

An interface declares what functions are required for a class to claim that it "implements Interfacename" whatever the Interface name is. In your case, you are
implements Comparable so you have to implement what the Comparable interface requires.

The interface does not tell you how to implement the functions, just that you must implement them.

How you do it is really an implementation detail, that does not have to be made public.
Ulrika Tingle
Ranch Hand

Joined: Nov 24, 2009
Posts: 92
Cheryl Scodario wrote:
I don't quite understand the codes above.

You're asking about very important basic things:

1. The role of the Object class (and the methods every class inherits from it including toString and more importantly equals and hashCode), and
2. the purpose if multiple inheritance (and the use of interfaces to achieve it with the specific example of the Comparable interface and how it relates to sorting).

I think you should ask your teacher for a proper clarification. Present the two points I listed and ask him to expand on these topics.
I agree. Here's the link:
subject: Comparable interface
It's not a secret anymore!