I am trying to write an abstract class. This class is going to be a Field. There will be different types of fields which will need to extend the field class and write its own setter class. For example....There will be a String Field and an Integer Field.
String Field will need to extend the Field class but must have it's own setValue
and getValue
method which has a String variable to set and return. Number Field must do the same with Integer.
I think the best way is to have a Generic Type set Value in the super class but I am not sure how to do about this. Any help much appreciated.
You can make your super class generic:
public abstract class Field<T>{
private T value;
public void setValue(T value){
this.value = value;
}
public T getValue(){
return value;
}
}
if you now extend it like:
public class StringField extends Field<String>{
//nothing to do here.
}
you are already done.
Like this:
public abstract class Field<T> {
private T value;
// + constructor(s)
public T get() {
return value;
}
public void set(T value) {
this.value = value;
}
}
public class StringField extends Field<String>{}
public class IntField extends Field<Integer>{}
You might want to add constructors such as Field(T value)
and StringField(String s)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With