I have an abstract superclass ValueType
and two non-abstract subclasses IntType
and DoubleType
. How can define types in the superclass method like following
public abstract class ValueType {
public abstract <T extends ValueType> T add(T other);
}
that I allows to write concrete types (e.g. public IntType add(IntType other)
or DoubleType add(DoubleType other)
) in subclass method
public class IntType extends ValueType {
private int val;
public IntType(int val){
this.val = val;
}
public int getVal(){
return this.val;
}
@Override
public IntType add(IntType other) {
return new IntType(this.val + other.getVal());
}
public class DoubleType extends ValueType {
private double val;
public DoubleType(double val){
this.val = val;
}
public double getVal(){
return this.val;
}
@Override
public DoubleType add(DoubleType other) {
return new DoubleType(this.val + other.getVal());
}
In the above example, the subclass method does not override the superclass method.