0

i have a class call Job which define (jobNumber,executionTIme,priorityLevel) (eg. for constructor Job) im going to use it for priorityQueue which is imported and used in other class call JobLine.

I have a method call compareTo() in Job class to compare the priority between Job object.

how should implements or extends Comparable to Job constructor so that PriorityQueue know which to compare the priority.

help is appreciated.

GETah
  • 20,922
  • 7
  • 61
  • 103
V_Stack
  • 147
  • 2
  • 7
  • 15
  • 2
    You can't `extend` `Comparable` as it is an interface, not a class. – Andrew Marshall Mar 05 '12 at 22:51
  • ok, in that case i should implements Comparable to overide the method compareTo to ? i tried and i got error. is there any other way? – V_Stack Mar 05 '12 at 22:56
  • There's no method to override as interfaces don't have method implementations. If you got an error you should include it in your question, along with the relevant code excerpts. – Andrew Marshall Mar 05 '12 at 22:57
  • @AndrewMarshall I think I see what you're getting at, but another `interface` could `extend` `Comparable`. I'm just being pedantic though, and you're right in saying that a `class` cannot `extend` an `interface` such as `Comparable`. – blahman Mar 06 '12 at 02:16

2 Answers2

3

By what you have written, you can simply implement (be aware, you can implement an interface but not extend it) it as this:

class Job implements Comparable<Job> {
    @Override
    public int compareTo(final Job other) {
        return getPriorityLevel() - other.getPriorityLevel();
    }
}

See this question to understand how to correctly implement the interface.

However, you can use some more "advanced" algorithms considering execution time, and job number, but you have to explain what you want if that's the case.

I didn't understand what you mean by "extend it to the constructor" in the question

Community
  • 1
  • 1
Francesco Belladonna
  • 11,361
  • 12
  • 77
  • 147
-1

A Class can be extended and an interface can be implemented

You can read this tutorial on how to go about implementing an interface.

noMAD
  • 7,744
  • 19
  • 56
  • 94