assassinweed2 assassinweed2 -4 years ago 113
Java Question

Non abstract class cannot override abstract method compareTo in Comparable?

I have a class

Vertex<T>
which implements
IVertex<T>
which implements
Comparable
. Whenever, I compile my code, I get the error:


Vertex is not abstract and does not override abstract method
compareTo(IVertex) in Comparable


The issue with this is that, I CANNOT change any of the code within the interface
IVertex
as that is what my teacher has instructed. How can I solve this issue? I have included my code below:

Vertex:

package student_solution;


import graph_entities.*;

import java.util.*;

public class Vertex<T> implements IVertex<T>{

// Add an edge to this vertex.

public void addEdge(IEdge<T> edge){

}

// We get all the edges emanating from this vertex:

public Collection< IEdge<T> > getSuccessors(){

}

// See class Label for an an explanation:

public Label<T> getLabel(){

}

public void setLabel(Label<T> label){

}

}


IVertex:

package graph_entities;

import java.util.Collection;

public interface IVertex<T> extends Comparable<IVertex<T>>
{

// Add an edge to this vertex.

public void addEdge(IEdge<T> edge);

// We get all the edges emanating from this vertex:

public Collection< IEdge<T> > getSuccessors();

// See class Label for an an explanation:

public Label<T> getLabel();

public void setLabel(Label<T> label);

}


Thank you in advance!

Answer Source

As the error suggests, your class implements an interface which extends Comparable. Now, in order to make your class concrete, you have to override all the methods of interfaces your class is implementing.

So, in your case, all you need to do is to override compareTo method in Vertex class, e.g.:

@Override
public int compareTo(IVertex<T> o) {
    // implementation
    return 0;
}

Here's Oracle's documentation on interfaces and inheritance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download