• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparable interface

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic