I want to have a constructor as shown below.
public Attribute(String attrName, Number attrValue){
this.name = attrName;
this.value = attrValue;
}
In this, I would like to have a method called incrementValue (Number n) that will add n to the value. I know that you cannot add two Number objects together due to possible casting problems. However, if I use validation to guarantee the value, and n is the same type, can they be combined together? Or maybe there is a better way to do this.
Now I declare Integer and Double instance variables and assign the value to the correct type. I was interested in expanding this to allow any subclass of Number. Obviously, I could write separate methods for each, but this seems like poor programming.
Is this possible in java? Do I really disagree about this?
source
share