Generic getter and setter methods

21,269

Solution 1

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.

Solution 2

Basic implementation

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>{}

for more details Please visit this link link2

Solution 3

I think what you've suggested is perfectly reasonable, it would look something like this:

public class Field<T extends Object> {

    private T value;

    public Field(final T value) { 
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(final T value) {
        this.value = value;
    }
}

Note, I have made this specifically not abstract on purpose. There's no need to use this as a super class unless you actually want to implement extra functionality for the specific field types. So you could define your Sting Fields and Number Fields like this:

Field<String> stringField = new Field<String>();
Field<Integer> numberField = new Field<Integer>();

Of course, if you do want extra implementations within the typed versions of Field, then make it abstract and define your subclasses as follows:

public class StringField extends Field<String> {
    public StringField(final String value) {
        super(value);
    }
}

public class NumberField extends Field<Integer> {
    public NumberField(final Integer value) {
        super(value);
    }
}

Solution 4

This looks like a really good practice to get acquainted with generics. Now there are a bunch of tutorials available so I'll try to explain a little bit what is happening and then I'll urge you too look up a few of them.

Basically you can think of generics as a way to tell your object during runtime which type(s) it will have, this is the type within the < and >.

So ArrayList<String> will be an ArrayList containing the type String. So going back to what you wan't to do is make your Field class generic.

This is easily accomplished with:

public class Field<T> {
    private T field;
}

Now when you create an instance of your Field class, say for a String, you'll simply write:

Field<String> f = new Field<>();

To write a generic getter with the same type as the type decided during runtime you will have do the following:

public class Field<T> {
    private T field;

    public T getField() { return field; }
}

I'll leave creating the setter as an excercise for you.

Remember that in this case T is a placeholder for the type that Field gets during runtime.

Solution 5

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).

Share:
21,269
user3859651
Author by

user3859651

Updated on March 21, 2020

Comments

  • user3859651
    user3859651 over 4 years

    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.

  • ChiefTwoPencils
    ChiefTwoPencils almost 10 years
    Where's the abstract class?
  • Dan Temple
    Dan Temple almost 10 years
    Fair question, I'll make my point more clear.
  • user3859651
    user3859651 almost 10 years
    Thank you to everyone who contributed. I have studied Generics but wasn't sure how to fit it into my scenario.