The simplest way is to redefine the attributes like you do in your first point.
protected Duration min;
protected Duration max;
In this way you would hide the attributes defined by the parent class. Since this is not a real redefnitiion you have to do more work in order to enforce their type. The setter methods have to be changed in order to only accept Duration
objects. You can do this like this:
@Override
public void setMin(ValueSpecification min_)
{
min = (min_ instanceof Duration) ? min_ : min;
}
@Override
public void setMax(ValueSpecification max_)
{
max = (max_ instanceof Duration) ? max_ : max;
}
The reason why your new setter-methods do not override the ones from Interval
is because they have different parameters and are therefore treated as individual methods (the concept is called overloading). In your code the original setter-methods would still be callable on objects from DurationInterval
. The above code on the other hand overrides the parent method and modifies them in order to only accept Duration inputs (others are just ignored; if you want you can throw an Exception instead).
As a side note: You should not define the attributes as public
because all object would have direct access to them and can put them in invalid states (e.g. null, negative, etc). Always go with private
or if you want them to be accessible in child classes with protected
.