Re: Generic Type Question
Defining subclass behaviour in a superclass seems a bit backwards. Are you really sure you need to subclass?
Cross/dot product, translation and enlargement can all be defined for the n dimensional case, for example. [Edit] which you are doing. Sure you would get a compile time check on the dimension, but folk manage to work with arrays (assign, pass, return) without having nanny compiler worry about the array length. Have a PointDimensionException and throw it, as you would ArrayIndexException, if anything goes wrong.
-----
Could you post a SSCCE of the Point class, its add() method and the Point2 subclass, and say what grumbling you are getting from the compiler?
Re: Generic Type Question
As far as I know cross product can only be calculated in three and seven dimensions and quaternion stuff needs 4 dimensions, thats why I made subclasses.
Code:
public abstract class Point
{
private float[] mValues;
public Point(float[] pValues)
{
mValues = pValues;
}
public float[] getValues()
{
return mValues;
}
public <T extends Point> T add(float pAdd)
{
float[] tNewValues = new float[this.getValues().length];
for (int i = 0; i < tNewValues.length; i++)
{
tNewValues[i] = this.getValues()[i] + pValue;
}
return (T) createSubClass(tNewValues);
}
public static Point createSubClass(float[] pValues)
{
switch (pValues.length)
{
case 2:
{
return new Point 2(pValues);
}
case 3:
{
return new Point3(pValues);
}
case 4:
{
return new Point4(pValues);
}
default:
{
return null;
}
}
}
}
Code:
public class Point3 extends Point
{
public Point(float[] pValues)
{
super(pValues);
}
public Point3 cross(Point3 pPoint)
{
Point3 tCross;
...
return tCross;
}
}
Error:
Code:
error: method setLocation in class Bla cannot be applied to given types;
setLocation(getLocation().add(pDistance));
required: Point3
found: Point
reason: actual argument Point cannot be converted to Point3 by method invocation conversion
Another weird thing is that the add() method of Point class has a return type "Point" although I put <T extends Point> and a class does not extend itself.
EDIT: Also I would like to keep the Point2/ Point3/ Point4 subclasses since they make the whole thing more readable and just nicer..
Re: Generic Type Question
Quote:
As far as I know cross product can only be calculated in three and seven dimensions and quaternion stuff needs 4 dimensions, thats why I made subclasses.
You're right, my bad.