Java inheritence with different typed variables

with the code below is there a way to make these two extremely similar classes inherit from a single superclass? they differ almost exclusively in variable types. If not is there a design pattern that seems most appropriate for this case? I've been looking at maybe using a factory pattern but I'm not sure if that's the most appropriate for the situation.

class stringCharacteristic {
    String name = "";
    String value = "";

    public String getValue() {
        return value;
    }

    public stringCharacteristic setValue(String value) {
        this.value = value;
        return this;
    }

    public String getName() {
        return name;
    }

    public stringCharacteristic setName(String name) {
        this.name = name;
        return this;
    }
}

class intCharacteristic {
    String name = "";
    int value = 0;

    public int getValue() {
        return value;
    }

    public intCharacteristic setValue(int value) {
        this.value = value;
        return this;
    }

    public String getName() {
        return name;
    }

    public intCharacteristic setName(String name) {
        this.name = name;
        return this;
    }
}

edit: Found a solution that works reasonably well for my purposes using generics, probably not the most efficient but it gets the job done. Also, I know the code looks a bit off with the fluid interface and everything, that's not the code that I'm actually writing just a simplified thing to show the issue without copy-pasting a hundred lines of stuff that just distracts from the main issue.

class characteristic<E> {
    String name;
    E value;
    int min = Integer.MIN_VALUE;
    int max = Integer.MAX_VALUE;

    public E getValue() {
        return value;
    }

    public characteristic<E> setValue(E value) {
        this.value = value;
        return this;
    }

    public String getName() {
        return name;
    }

    public characteristic<E> setName(String name) {
        this.name = name;
        return this;
    }

    public characteristic<E> setMin(int min) {
        this.min = min;
        return this;
    }

    public characteristic<E> setMax(int max) {
        this.max = max;
        return this;
    }

    public boolean validate() {
        if (value instanceof String) {
            return ((String) value).length() <= max && ((String) value).length() >= min;
        }
        if (value instanceof Integer) {
            return (int) value <= max && (int) value >= min;
        }
        if (value instanceof Boolean) {
            return true;
        }
        if (value instanceof Double) {
            return (Double) value <= max && (Double) value >= min;
        }
        return false;
    }
}


Read more here: https://stackoverflow.com/questions/68462260/java-inheritence-with-different-typed-variables

Content Attribution

This content was originally published by Yehudah Goldstein at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: