Vector.java:
public interface Vector<T extends Vector> {
public int getX();
public int getY();
public int getZ();
public T sum(T other);
}
BaseVector.java:
public abstract class BaseVector<T extends Vector> implements Vector<T> {
private final int x;
private final int y;
private final int z;
public BaseVector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
protected abstract T createNew(int x, int y, int z);
@Override
public T sum(T other) {
return createNew(x + other.getX(), y + other.getY(), other.getZ());
}
@Override
public int getX() {
return x;
}
@Override
public int getY() {
return y;
}
@Override
public int getZ() {
return z;
}
@Override
public String toString() {
return "BaseVector [x=" + x + ", y=" + y + ", z=" + z + "]";
}
}
Velocity.java:
public class Velocity extends BaseVector<Velocity> {
public Velocity(int x, int y, int z) {
super(x, y, z);
}
@Override
protected Velocity createNew(int x, int y, int z) {
return new Velocity(x, y, z);
}
}
Test.java:
public static void main(String[] args) {
Velocity v1 = new Velocity(14, 14, 14);
Velocity v2 = new Velocity(14, 14, 14);
Velocity result = v1.sum(v2);
System.out.println(result);
}
}