Can I require classes implementing an interface to have a certain static field or method and access/invoke that field or method through a generic type argument?
I have an interface, Arithmetical<T>
, which specifies several functions like T plus(T o)
and T times(T o)
. I have as well a Vector<N extends Arithmetical<N>>
class, which is intended for vectors (of variable dimension) with components of type N
. I ran into an issue, however, when trying to implement the dot product.
I want to implement the method N dot(Vector<N> o)
. For this, I plan to start with whatever N
's zero is and iterate through both Vector<N>
s' List<N>
s, adding the product of each pair of elements to my total. Is there a way to specify in Arithmetical<T>
that all implementing classes must have a static (and preferably final) field ZERO
and start dot(Vector<N> o)
's body with something along the lines of N sum = N.ZERO;
?
If not, what other approaches might there be to this problem? I want to allow 0-dimensional vectors, so I can't just begin by multiplying the vectors' first components. Is there a way to instantiate an object of a generic type, so I can merely specify a T zero()
method in Arithmetical<T>
?
I have a reason for not using Java's numerical types—I want to have vectors with complex components.
Here's Arithmetical:
public interface Arithmetical<T> {
public T plus(T o);
public T minus(T o);
public T negate();
public T times(T o);
public T over(T o);
public T inverse();
// Can I put a line here that requires class Complex (below) to define ZERO?
}
Vector:
public class Vector<N extends Arithmetical<N>> {
private List<N> components;
public Vector<N>(List<N> cs) {
this.components = new ArrayList<N>(cs);
}
public N dot(Vector<N> o) {
// Here's where I need help.
}
}
And Complex:
public class Complex implements Arithmetical<Complex> {
public static final Complex ZERO = new Complex(0, 0); // Can I access this value through N if <N extends Arithmetical<N>>?
private double real;
private double imag;
public Complex(double r, double i) {
this.real = r;
this.imag = i;
}
/* Implementation of Arithmetical<Complex> (and some more stuff) not shown... */
}
I'm quite new to Java (and programming in general); I will likely not understand complex (ha) explanations and workarounds.
Thanks!
(Python is a suggested tag... Huh.)