I am tempted to do such kind of code, using jGraphT
/*
interface DirectedGraph<V,E> { ...}
interface WeightedGraph<V,E> { ...}
*/
public class SteinerTreeCalc {
public SteinerTreeCalc( < ??? implements DirectedGraph<V,E>, WeightedGraph<V,E> > graph ) {
......
}
}
I want to create a constructor that ask for an object implementing two interfaces.
Update :
In my goal, there are already chosen classes for Vertex and Edges (V and E), but thanks a lot to people who come up with :
public class SteinerTreeCalc <V, E, T extends DirectedGraph<V, E> & WeightedGraph<V, E>>
{
....
}
This could be what you want:
Hidden Features of Java
you can use extends instead of implements in above code
Yes, it's possible:
If
V
andE
are concrete classes rather than type parameters, then you could create a new interface as follows:then
The problem is that the actual argument must implement the
DirectedWeightedGraph
interface. A type that just implementsDirectedGraph<V,E>
andWeightedGraph<V,E>
is not sufficient.Should work like this, but this is complexer generics logic, hope you can adapt:
These are the interfaces and the constructor like asked in your question.