Reputation: 125
I was implementing a Java Graph library (to learn ...). Accordingly, I wrote an interface
public interface DigraphInterface {
public boolean isEmpty();
public int size();
public boolean isAdjacent(Object v, Object w);
public void insertEdge(Object v, Object w);
public void insertVertex(Object v);
public void eraseEdge(Object o, Object w);
public void eraseVertex(Object v);
public void printDetails();
}
As the first step towards implementing, I am writing Digraph class that implements above interface. However, to keep things simple, I want the node identifiers as integers, so I defined functions as
@Override
public boolean isAdjacent(int v, int w) {
// TODO Auto-generated method stub
return adjList[v].contains(w) || adjList[w].contains(v);
}
But, I am getting error, that I need to override or implement method with supertype. Can someone explain me the underpinnings for this behavior. Also, if someone can explain, how do we design libraries that allow flexibility to add components of any type.
Upvotes: 5
Views: 3909
Reputation: 23903
You interface says:
public boolean isAdjacent(Object v, Object w);
you implement:
public boolean isAdjacent(int v, int w)
for java this hasn't the same signature, therefore isn't the same method. What you could do is to use generics, it depends on what you need but, in this case you could do something like:
public interface DigraphInterface<T> {
...
public boolean isAdjacent(T v, T w);
...
}
and your implementation could be:
public class DefaultDigraph<Integer> {
...
public boolean isAdjacent(Integer v, Integer w) {
...
}
...
}
of course you need to take care, because Integer
can be null, and int
not. Therefore null checks over parameters would be a good idea, before its auto-unboxing.
Upvotes: 9