Reputation:
I'm doing some graph implementation and I think the code smells quite bad.
So I made an interface Graph:
interface Graph <K extends Comparable <K> , V>
which for example have a method like this:
public void addVertex(K key, V value);
or
public boolean remove (K key);
The implementations will hold key value pair based vertices.
Then I made an actual class:
public class AdjacencyList <K extends Comparable <K> , V> implements Graph
what caused my above mentioned methods to something like this:
public void addVertex(Comparable key, Object value){/*rest of the code*/}
public boolean remove(Comparable K){/*rest of the code*/}
These makes me do a lot of casting of course.
I'm just learning wildcards, scratching the surface, read a few questions here, however I'm still confused why my code behaves like this, and more importantly why, what is the proper way to things like this to make this clean, and make the method parameters back to K and V.
Upvotes: 1
Views: 426
Reputation: 82899
You have to change your class to implement Graph<K, V>
instead of just Graph
.
public class AdjacencyList <K extends Comparable<K>, V> implements Graph<K, V>
Then you can use K
and V
for your implemented methods' parameters.
@Override
public void addVertex(K key, V value) {
// TODO Auto-generated method stub
}
Upvotes: 1