generics with subclass
<p>I have three different types of parameters: int, float and long. I want to use an object to represent each of them. So i have one abstract class: </p>

<pre><code>abstract public class AbstractProtocolParamObj&lt;T extends Number&gt;
{
    public enum ProtocolParamConstraintTypeEnum
    {
        None,
        OnOff,
        Values,
        ValueRange,
        ValueRangeIncrement
    }

    protected String name;
    protected ProtocolParamConstraintTypeEnum constraintValueType;
    protected Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; dependentParams;
    protected Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; constraintParams;
    protected T value;

    protected AbstractProtocolParamObj(String name, 
            ProtocolParamConstraintTypeEnum constraintValueType,
            T value)
    {
        this.name = name;
        this.constraintValueType = constraintValueType;
        this.value = value;
    }

    public final String getName()
    {
        return name;
    }

    public final ProtocolParamConstraintTypeEnum getConstraintValueType()
    {
        return constraintValueType;
    }

    public void addDependentParam(AbstractProtocolParamObj&lt;T&gt; param)
    {
        if(dependentParams == null)
        {
            dependentParams = new Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt;();
        }
        dependentParams.add(param);
    }

    public void addConstraintParam(AbstractProtocolParamObj&lt;T&gt; param)
    {
        if(constraintParams == null)
        {
            constraintParams = new Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt;();
        }
        constraintParams.add(param);
    }

    public Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; getDependentParams()
    {
        return dependentParams;
    }

    public Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; getConstraintParams()
    {
        return constraintParams;
    }

    public T getValue()
    {
        return value;
    }

    public void setValue(T val)
    {
        value = val;
    }

    abstract public ReturnStatusEnum validate(T tempVal);   
}
</code></pre>

<p>Then I will have one class for float parameters, one class for int and one for long. Like this:</p>

<pre><code>    public class ProtocolFloatParamObj extends AbstractProtocolParamObj
{
    private float[] constraintVals;
    private float maxVal;
    private float minVal;
    private float increment;

    public ProtocolFloatParamObj(String name,
                            float value,
                            float[] constraintVals,
                            ProtocolParamConstraintTypeEnum constraintType
    )
    {
        super(name, constraintType, value);
         this.constraintVals = constraintVals;
    }

    public ProtocolFloatParamObj(String name,
            float value,
            float maxVal,
            float minVal,
            ProtocolParamConstraintTypeEnum constraintType
    )
    {
        super(name, constraintType, value);
        this.maxVal = maxVal;
        this.minVal = minVal;
    }

    public ProtocolFloatParamObj(String name,
            float value,
            float maxVal,
            float minVal,
            float increment,
            ProtocolParamConstraintTypeEnum constraintType
    )
    {
        this(name, value, maxVal, minVal, constraintType);
        this.increment = increment;  
    }

    @Override
    public ReturnStatusEnum validate(Number val)
    {
        ReturnStatusEnum status = ReturnStatusEnum.SUCCESS;
        float tempVal = val.floatValue();

        switch(constraintValueType)
        {
            case None:
            {
                break;
            }
            case OnOff:
            {
                break;
            }
            case Values:
            {
                                    break;
            }
            case ValueRange:
            {
                                    break;
            }
            case ValueRangeIncrement:
            {
                                    break;
            }
        }

        return status;
    }
}
</code></pre>

<p>Above code has no compile error but do has warnings which complain the generic type should be parameterized in subclass at the following lines:</p>

<pre><code>public class ProtocolFloatParamObj extends AbstractProtocolParamObj
super(name, constraintType, value);
</code></pre>

<p>But if i change </p>

<pre><code>public class ProtocolFloatParamObj extends AbstractProtocolParamObj
</code></pre>

<p>to </p>

<pre><code>public class ProtocolFloatParamObj &lt;T extends Number&gt; extends AbstractProtocolParamObj&lt;T&gt;
</code></pre>

<p>and change the constructor <code>value</code> parameter from <code>float</code> to <code>T</code>. then everything looks good without compile error and warning.</p>

<p>The issue is because the ProtocolFloatParamObj constructor has a T parameter, its user/caller needs to define the <code>T</code> and it's easy to get warning or compile error. </p>

<p>For example in another class, I try to create a Vector contains some of those parameter objs but I cannot eliminate warnings or I cannot add to the <code>objs</code> vector:</p>

<pre><code>public Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; getAxialParamObjs()
    {
        Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt; objs = new Vector&lt;AbstractProtocolParamObj&lt;T&gt;&gt;();
        ProtocolFloatParamObj&lt;T&gt; scanSpeed = new ProtocolFloatParamObj("scanSpeed", 
                new Float(Float.parseFloat(m_axialDefaultConfig.getProperty("scanSpeed"))),
                new float[]{0.5f, 0.8f, 1.0f, 1.5f, 2.0f},
                ProtocolParamConstraintTypeEnum.Values
                );

        objs.add(scanSpeed);
        ......
        return objs;
    }
</code></pre>

<p>It seems this is not a good idea or i need read more about java generic.</p>

<p>Do you have a better idea and do you have any advanced java generic tutorial links?</p>
