Reputation: 436
I am implementing Dijkstra Algorithm using Priority Queue in java. I am using objects of a class 'Node' for the queue. To implement this I am overriding the compareTo() method in Node class. It is working fine if I am using "Object obj" as parameters of compareTo(). However when I am giving "Node obj" as parameters to compareTo() an error is being diplayed saying "Node is not abstract and does not override abstract method compareTo(Object) in Comparable." From what I have read this should be possible. What is wrong?
public class Node implements Comparable
{ int key;
int val;
public Node(int v)
{ val=v;
key=0;
}
@Override
public int compareTo(Object obj)
{ Node nd=(Node) obj;
return this.key-nd.key;
}
public Node(int v, int k)
{ val=v;
key=k;
}
}
The error is being displayed if I do this(the code below) while the above code is working fine.
@Override
public int compareTo(Node obj)
{
return this.key-obj.key;
}
Upvotes: 1
Views: 494
Reputation: 131456
Comparable is an interface that specifies a parameterized type :
public interface Comparable<T>{...
We can also say that is a generic class (shorter).
So declaring public class Node implements Comparable{...
means that you want to implement Comparable with not a specific type.
In fact for the compiler public class Node implements Comparable{
is the same thing as
public class Node implements Comparable<Object>{
So, the compiler expects to find a public int compareTo(Object obj)
method in your class.
Whereas the compilation error message :
"Node is not abstract and does not override abstract method compareTo(Object) in Comparable."
Specify the class that you want to use in the Comparable interface you are implementing : (here public class Node implements Comparable<Node>{
).
And the compiler will expect to find the implemented method:
public int compareTo(Node obj)
Upvotes: 2
Reputation: 8246
Because the interface (Comparable) you have overridden is not typed. You need to use Java Generics to declare the type you want to compare:-
public class Node implements Comparable<Node>
This says that you want types of Node
to be Comparable
and will allow you to do
public int compareTo(Node obj)
If you don't genericise it, then you are effectively doing an @Override
then changing the method signature, which your compiler will flag as an error because you are doing an @Override
on a method not in the interface.
Upvotes: 1
Reputation: 5695
Do this instead,
public class Node implements Comparable <Node>{
This is because Comparable is a generic interface, and as stated by the docs takes a parameter T
T
- the type of objects that this object may be compared to
Without this parameter, it resolves down to Object
, which is something you don't need.
Upvotes: 1
Reputation: 272750
The interface that you are trying to implement - Comparable
, is generic. If you do not specify any generic arguments, the generic argument defaults to Object
.
According to the interface, a Comparable<Object>
should have this method:
public int compareTo(Object o);
But in your code, you only have this method:
public int compareTo(Node obj);
The compiler thinks that they are different and so complains.
To fix this, just specify a generic type argument. It's nothing difficult:
public class Node implements Comparable<Node>
Upvotes: 0
Reputation: 122008
Interface Comparable<T>
Is a Generic interface and since you are not binding any type the default is Object bind the required type (Comparable<Node>
) and you'd be fine.
Upvotes: 0